Perform action when segue is closed

时光总嘲笑我的痴心妄想 提交于 2019-12-24 09:38:57

问题


I have a ViewController where I call segue to another ViewController:

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        if segue.identifier == blogSegueIdentifier {
            let destination = segue.destination as! Reply
            let path = tableView.indexPathForSelectedRow
            let cell = tableView.cellForRow(at: path!) as! QuestionsTabCell
            destination.blogName = String(cell.labelQues!.tag)
        }
    }

In the opened segue (ViewController) there is a UITextView and a send button in the UINavigationBar. So user inputs textView and sends it to server tapping on the button and after this opened segue (ViewController) closes and user returns to first ViewController. How to call a method in first ViewController when user closes segue without using viewWillAppear?


回答1:


Hello @Mr Jo You can use unwind segue for this purpose.

declare a method in your first viewController Like :-


    //MARK: - - Unwind Segue get data from select categories
    @IBAction func unwindToAssignCatController(segue: UIStoryboardSegue) {

        //get back data from selectcategory class
        if segue.identifier == "segueIdentifier"{

            let controller = segue.source as! Reply
            // get your values from second viewController and use in first viewController
        }


After that you have to select your second ViewController

1: right Click on Controller and drag on exit then below method will appear 2: Select it and assign an identifier to it. 3: then perform segue on that action where you want in second viewController with same identifier.




回答2:


If you have the NavigationController, try: (Objective-C)

FirstVC* firstVC = self.navigationController.viewControllers[self.navigationController.viewControllers.count
- 2]

last but one in a series of viewControllers in the navigation stack

If you are opening the second viewController without the NavigationController, you can get the instance of the first viewController like:

FirstVC* firstVC = self.presentingViewController

Please not that the instance of the presenting VC will be available in this property after the transition will finish, in the viewDidAppear, for example



来源:https://stackoverflow.com/questions/48060720/perform-action-when-segue-is-closed

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!