user577732
user577732

Reputation: 4046

Dismiss one modal view controller after showing another

I've done a bit of research and read other answers I found here but haven't found anything that actually works. I have an app that when something is posted I want to go to the post and if the back button is pressed when viewing the post it should go back two views basically skip over the compose view.

Below is what I've tried but it gives

Warning: Attempt to present on whose view is not in the window hierarchy!

-(IBAction)post{
    [[self presentingViewController] dismissModalViewControllerAnimated:NO];
}

-(void)viewDidDisappear:(BOOL)animated{
    [super viewDidDisappear:animated];
    ViewPostViewController *dvController = [[ViewPostViewController alloc] initWithNibName:@"ViewPostViewController" bundle:[NSBundle mainBundle]];
    UINavigationController *nc = [[UINavigationController alloc] initWithRootViewController:dvController];
    nc.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
    [self presentModalViewController:nc animated:NO];
    [nc release];
}

Upvotes: 1

Views: 3113

Answers (3)

TheEnigma2112
TheEnigma2112

Reputation: 173

If you trying to create a hierarchy of views like this, you should probably be using these:

[self.navigationController pushViewController:ViewController animated:BOOL completion:nil];
[self.navigationController popViewControllerAnimated:BOOL completion:nil];

Instead of:

[self presentViewController:ViewController animated:BOOL completion:nil];
[self dimissViewControllerAnimated:BOOL completion:nil];

PresentViewController is usually used to show a single view controller then dismiss it, not generally when you want to show several view controllers in a chain, then work your way back up the chain.

The former is advisable because it uses the stack concept to push and pop view controllers. So you can start with your initial list set up as the root view controller, push on your post compose view, then push on the third view to go to posting. Then when you want to go back to the first view controller by popping off two view controllers, you can use:

[self popToRootViewControllerAnimated:BOOL completion:nil];

You might find the UINavigationController reference useful.

Good luck.

Upvotes: 3

Cocoanetics
Cocoanetics

Reputation: 8247

If you want to present a view controller right after another modal view controller has animated out then you have to delay it because otherwise the new one will not appear.

before iOS 5 you would do a performSelectorAfterDelay: with something like 0.25 sec. For iOS 5 and above you wouldn't use modelViewController methods any more as those have been deprecated. Instead you use the presentViewController methods which give you an completion block that is called when the animation is done.

Upvotes: 1

rdelmar
rdelmar

Reputation: 104082

I'm a little confused about what you're trying to do. If you're using a navigation controller, you should be doing pushes and pops, not presenting and dismissing. If you want to use navigation controllers, then you can use popToViewController:animated: to go back to any particular controller without passing through the ones in between. You would have to create a custom back button, though, or do it in code, because the standard back button will only take you back to the previous view controller.

Upvotes: 0

Related Questions