segue won't unwind

I control-dragged a bar button item on the navigation bar of a table view controller that uses a navigation controller to the exit icon of the table view controller. I was able to select an unwind function for it. However, when I run the code, it won't unwind. when I click the bar button item, it doesn't do anything. Neither the prepareForSegue nor the unwind function is executed. What should I check?

Accepted Reply

Have you defined the IBAction in the first view controller ?


If you want to learn more, may have a look at :

h ttps://www.andrewcbancroft.com/2015/12/18/working-with-unwind-segues-programmatically-in-swift/

Replies

When you right click in IB on the button, do you see the triggered segue reference ?


In the view controller, you need to have :


    @IBAction func unwindToViewController(sender: UIStoryboardSegue) {      
          // No  need to have any code inside
    }

Yes. I see it.

Have you defined the IBAction in the first view controller ?


If you want to learn more, may have a look at :

h ttps://www.andrewcbancroft.com/2015/12/18/working-with-unwind-segues-programmatically-in-swift/

I think that was the problem. I created an IBAction for the bar button item and it now works. Thank you.

Now the code in the IBAction for the bar button item isn't running.

Can you show the code ?

Did you insert a print at the beginning to check it's being called ?

Yes, I put a print at the beginning. It isn't executed.


    @IBAction func actionUnwindToMainViewController(_ sender: UIBarButtonItem) {
       
        print("actionUnwindToMainViewController(_:)")
       
        let appDelegate = UIApplication.shared.delegate as! AppDelegate
       
        let managedContext = appDelegate.persistentContainer.viewContext
       
        let entity = NSEntityDescription.entity(forEntityName: "Recipient", in: managedContext)!
        print("contacts.count = \(contacts.count)")
       
        for contact in contacts {
           
            if contact.flagImport {
               
                let recipient = NSManagedObject(entity: entity, insertInto: managedContext)
               
                recipient.setValue(contact.name, forKey: "name")
                recipient.setValue(contact.phoneNumber, forKey: "phoneNumber")
                recipient.setValue((contact.providerId), forKey: "providerId")
               
            }
           
        }
       
        do {
           
            try managedContext.save()
           
        } catch let error as NSError {
           
            print(error)
           
        }
       
/
/
/
/
/
/
/
/
/ 
    }

Now the segue unwinds without there being an IBAction for the bar button item. I was able to delete it. This is very erratic.