Run when you come back from Popover

Asked 2 years ago, Updated 2 years ago, 156 views

How do I detect it when I come back from Popover?
You can't use viewWillAppear, etc.
Run function during screen transition
swift I tried to run the function when I dismissed by referring to , but when I tried to use the variable in the viewController on the return side, I got nil back.
Please let me know if anyone knows.

Code

override functableView(_tableView:UITableView, didSelectRowAtindexPath:IndexPath){
    // Unselect Cells
    tableView.deselectRow(at:indexPath, animated:true)

    letback=storyboard!.instantiateViewController(withIdentifier: "View") as? ViewController
    back?.doAnything (text:text [indexPath.row])
    self.dismiss (animated: true, completion: {() in back?.received=self.text [indexPath.row]})
}

Another one

 var number: Int!

func doAnything (text: String) {
    print(text)
    switch text {
    case "1":
        let activityItem: [Int] = number // This becomes nil
        let activityVC = UIAactivityViewController (activityItems:activityItem, applicationActivities:nil)
        self.present(activityVC, animated:true, completion:nil)

        print(url)

    case"2":
        break
    case "3":
        break
    case "4":
        break
    case "5":
        break
    default:
        break
    }
}

swift xcode swift5

2022-09-30 21:39

1 Answers

Just to confirm, I expect your code to look like this now.

Calling view controller:

class ViewController:UIViewController {

    //...

    // Assume Popover is displayed as `present` from some action method...
    @ IBAction func someAction(_sender:Any){
        //...
        let storyboard = UIStoryboard (name: "Main", bundle:nil)
        let popover=storyboard.instantiateViewController(withIdentifier: "Popover") as!PopoverViewController
        //...
        self.present(popover, animated:true, completion:nil)
    }

    var received —String?=nil

    var number —Int!

    func doAnything (text: String) {
        print(text)
        switch text {
        case "1":
            let activityItem: [Int] = [number] // <-`[number]`?
            let activityVC = UIAactivityViewController (activityItems:activityItem, applicationActivities:nil)
            self.present(activityVC, animated:true, completion:nil)

            // print(url)//<- What is `url`?

        case"2":
            break
        case "3":
            break
        case "4":
            break
        case "5":
            break
        default:
            break
        }
    }
}

view controller for Popover:

class PopoverViewController:UITableViewController {

    //...

    var text: String = [ ]

    override functableView(_tableView:UITableView, didSelectRowAtindexPath:IndexPath){
        // Unselect Cells
        tableView.deselectRow(at:indexPath, animated:true)

        letback=storyboard!.instantiateViewController(withIdentifier: "View") as? ViewController
        back?.doAnything (text:text [indexPath.row])
        self.dismiss (animated: true, completion: {() in back?.received=self.text [indexPath.row]})
    }

    //...
}

There were some parts of the code you indicated that could not be compiled, so I have corrected it.It may have been rewritten a little to ask questions, but please be aware that the results may be fundamentally different.It also includes some estimates.If there are any significant differences, please let us know by adding information to your question or commenting.

So I think this is where your code is fatally incorrect.

letback=storyboard!.instantiateViewController(withIdentifier: "View") as? ViewController

You must not create a new instance to access the view controller that Popover returns to.This code creates a new instance (although it is in the same ViewController class), and the created instance is different from the view controller in "Return to".

There are many ways to do this, but the surest thing is to let Popover have the return view controller directly as a property.

Popover looks like this.

protocol PopoverViewControllerDelegate:class{
    var received: String?{getset}
    func doAnything (text: String)
}
class PopoverViewController:UITableViewController {

    //...

    weak vardelegate —PopoverViewControllerDelegate?

    var text: String = [ ]

    override functableView(_tableView:UITableView, didSelectRowAtindexPath:IndexPath){
        // Unselect Cells
        tableView.deselectRow(at:indexPath, animated:true)

        delete?.doAnything (text:text[indexPath.row])
        self.dismiss(animated:true){
            self.delegate?.received=self.text [indexPath.row]
        }
    }

    //...
}

This is a typical delete pattern, so the return destination is delegate and the features you want to invoke (methods and properties) are protocol.

This is what the caller will look like.

//↓Added conformity to `PopoverViewControllerDelegate`
classViewController:UIViewController, PopoverViewControllerDelegate{

    //...

    @ IBAction func someAction(_sender:Any){
        //...
        let storyboard = UIStoryboard (name: "Main", bundle:nil)
        let popover=storyboard.instantiateViewController(withIdentifier: "Popover") as!PopoverViewController
        popover.delegate=self//<- Set to delete when Popover is called
        //...
        self.present(popover, animated:true, completion:nil)
    }

    var received —String?=nil

    var number —Int?//<- Do not use ImplicitlyUnwrappedOptions for variables that may not be initialized

    func doAnything (text: String) {
        Verify // `number` is non-nil with guard
        guard let number = number else {
            print("`number`is nil")
            return
        }
        print(text)
        //...
    }
}

You should not use ImplicitlyUnwrappedOptions for variables such as number that may be uninitialized depending on the order of calls, so you should change it to a normal optional one and check non-nil with guard before using it.

As I wrote at the beginning, it contains some estimates, so I may have shown you something that is completely useless, but if you can apply it well to your code, please try it.


2022-09-30 21:39

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.