Reputation: 692
I have three UIViewControllers (let's call them A, B and C) in a navigation controller. A can segue into either B or C. B can segue into C. When C closes, I want it to always return to A, i.e. it automatically closes B upon closing C, if opened from B.
Now, I tried using segue unwinding, so that when C closes, B's return method gets called to dismiss the destination controller:
- (IBAction)returnFromC:(UIStoryboardSegue*)segue
{
[segue.destinationViewController dismissViewControllerAnimated:YES completion:nil];
}
I put a breakpoint in this method - it is called. I have verified that the destinationController is indeed B. However, I noticed when the break point hits, C is still visible. After playing from the break point, C does exit as expected, but B is still visible.
Any ideas? Many thanks in advance.
Upvotes: 0
Views: 376
Reputation: 3734
dismissViewControllerAnimated:completion:
is used to dismiss a Modal segue and has no effect for Push segues
As @matt suggested you could just remove B (the middle) view controller from self.navigationController.viewControllers
and here's a sample code that you can put in B view controller:
- (void)viewDidDisappear:(BOOL)animated {
[super viewDidDisappear:animated];
// No need to process if the viewController is already being removed
if (!self.isMovingFromParentViewController) {
// Getting a mutable copy of the viewControllers (can't directly modify)
NSMutableArray *temp = [self.navigationController.viewControllers mutableCopy];
// Removing 'self' -> so A > B > C will become A > C
[temp removeObject:self];
// Setting the new array of viewControllers
self.navigationController.viewControllers = [temp copy]; // getting an immutable copy
}
}
P.S. You could use popViewControllerAnimated:
instead of Unwind segue if you're not planning to send the data back.
Upvotes: 0
Reputation: 534885
When C closes, I want it to always return to A, i.e. it automatically closes B upon closing C, if opened from B
The simplest solution is: in C's viewDidAppear:
, secretly remove B as a child of the navigation controller. In other words, you've got this:
A > B > C
Now you rearrange things so that you've got this:
A > C
Thus, the only thing to go back to from C is A.
It's easy to manipulate the navigation controller's set of children in this way. Just call setViewControllers:animated:
(with a second argument of NO).
[But if I've understood your setup properly, another easy way would be to implement the unwind method in A and not B. Then do an unwind segue. We always unwind to the first view controller that contains the unwind method, so that would always be A.]
Upvotes: 3