Search code examples
iosswiftuitableviewsegue

Perform action when segue is closed


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?


Solution

  • 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.

    enter image description here