Darren
Darren

Reputation: 1133

iOS Unwind Segue stops working when embedded in a navigation controller

I have a UIViewController which has a button that performs a modal segue to a UIViewController as a Form Sheet. On that form sheet is a cancel button with an unwind segue back to the original UIViewController. This seems to work perfectly happily and dismisses the form sheet.

As soon as I select the original UIViewController and select the option in XCode to Embed in a Navigation Controller, the unwind segue no longer seems to work and the modal form sheet will no longer cancel.

I'm sure there is a simple explanation, but it is currently evading me, so any thoughts welcomed.

Thanks.

Upvotes: 7

Views: 3187

Answers (3)

Vitalii Boiarskyi
Vitalii Boiarskyi

Reputation: 1393

 self.dismissViewControllerAnimated(true, completion: nil)

"dismiss" didn't work for me but popToRoot works well.

 [self.navigationController popToRootViewControllerAnimated:YES];

Upvotes: 0

Jakob Hviid PhD
Jakob Hviid PhD

Reputation: 115

This is already answered, but the above did not give me the full solution, so i thought i would share mine.

For me the unwind method was never called, so i could not do what was described above. Instead i made a simple button with an IBAction and called it. Swift example below:

@IBAction func cancelToRecipeController() {
    self.dismissViewControllerAnimated(true, completion: nil)
    print("cancel to recipe controller ran")
}

Thanks to @rdelmar above for the method call!

(This was added for ios 9)

Upvotes: 0

rdelmar
rdelmar

Reputation: 104082

I don't know why you get the behavior you're seeing, I see it too. It happens for both the page sheet and form sheet presentations, but not for full screen. However, the unwind segue is still calling the method you put in the original controller (the one you connected the unwind segue to). So, all you need to do is put a line in that method:

[self dismissViewControllerAnimated:YES completion:nil];

Upvotes: 13

Related Questions