Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Modal View Controller, dismiss and pop back to view controller

Im trying to dismiss a ModaViewController called C, back to A. The ModalViewController is presented in B. Therefore the Navigation flow is A->B - (present ModalView) -> C. I am able to dismiss the ModalViewController back to B, but I am unable to pop back to A in the completion bracket. This is the code I have tried:

[self dismissViewControllerAnimated:YES completion:^{
    [self.navigationController popToViewController:[[self.navigationController viewControllers] objectAtIndex:0] animated:YES];
}];

The ModalViewController is dismissed but does not pop back to A. I call this block of code in an IBAction.

Any advice?

On a second note, when I dismiss the ModalViewController all my UIPickers in Controller B are empty/ deallocated. I am using ARC as well.

like image 759
DevC Avatar asked Apr 02 '14 15:04

DevC


1 Answers

The problem with your code is that self.navigationController will be nil. If you have a controller (A) embedded in a navigation controller, and that controller pushes to another controller (B) which then presents your last controller (C), then you need to do something like this,

-(IBAction)dismissToBThenPop:(id)sender {
    UINavigationController *nav = (UINavigationController *)self.presentingViewController;
    [self dismissViewControllerAnimated:YES completion:^{
        [nav popViewControllerAnimated:YES];
    }];
}

Even though you present C from B, the actual presentingViewController will be the navigation controller. This code will dismiss C then pop B, but you will see B for an instant before ut pops back to A. If you don't want to see this, then you should use an unwind segue to go directly back to A from C.

Your second problem about the pickers being empty and deallocated should not be happening under the scenario that you say you have. You will have to provide more information about what you're doing in B to solve that problem.

like image 94
rdelmar Avatar answered Oct 22 '22 20:10

rdelmar