Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to do two UINavigationController opertaion from the same method?

Tags:

iphone

Often I find myself need to dismiss the current modal view controller and then immediately push another view controller (so imagine something sliding down and then sliding right). What is a clever way to do this?

If you attempt to do these two operations all in one go, only the first one is carried out, the second one is ignored.

-(void)dismissThenPush{
[self.navigationController dismissModalViewControllerAnimated:TRUE]; //works
[self.navigationController pushViewController:controller animated:TRUE];    //ignored
}

For the past 12 months, what I do is set some global flag, and have the original controller check for this flag in the viewDidAppear method and then call a pushViewController method. But I'm tired of this hack, there must be a way to do this all in one go from the modal view controller.

like image 959
erotsppa Avatar asked Dec 06 '25 14:12

erotsppa


1 Answers

I just played around with this. I ended up with this:

  1. Pass the (original) view controller to the modal view controller via a property. The next view controller (that will need to be pushed) could also be passed as a property (if it's not always the same).
  2. Present the modal view.
  3. In the modal view controller method that dismisses the modal view, also push the next view controller by accessing the navigation controller from the original view controller property.

You end up with the sliding down and sliding right animations happening at the same time, but I think it looks ok.

Code:
In the original view controller:

- (IBAction)pushModalVC {
    ModalViewController *modalVC = [[ModalViewController alloc] init];

    modalVC.ownerVC = self;

    [self presentModalViewController:modalVC animated:YES];

    [modalVC release];
}

In the modal view controller:

- (IBAction)pushSecondVC {
    // this could be accessed via a property rather than loading
    SecondViewController *secondVC = [[SecondViewController alloc] init];

    [self dismissModalViewControllerAnimated:YES];

    [ownerVC.navigationController pushViewController:secondVC animated:YES];

    [secondVC release];
}
like image 68
gerry3 Avatar answered Dec 08 '25 08:12

gerry3



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!