1

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];
}
Albert Renshaw
  • 17,282
  • 18
  • 107
  • 195
user577732
  • 3,956
  • 11
  • 55
  • 76

3 Answers3

3

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.

TheEnigma2112
  • 173
  • 1
  • 7
  • I agree with this for an app using nav controllers, however this won't work if the root is a SplitViewController, for example. Any solution for that? – Kyle Clegg Jan 29 '13 at 21:14
  • I'm not sure that I understand your question, but from what I've read, Apple insists that you should always have your SplitViewController visible in your app; no pushing or popping. This might help: [http://stackoverflow.com/questions/2633532/navigate-to-a-splitviewcontroller] – TheEnigma2112 Feb 01 '13 at 00:17
  • I believe the requirement is that it's always root, not necessarily always visible -- modal views are fine. In my case I am displaying a modal view and a sequence of 3 screens, so I embedded a nav controller into that modal view. I got it working by dismissing the entire nav controller after the third screen, and it returns to the splitViewController as I'd hoped. – Kyle Clegg Feb 01 '13 at 00:22
  • Sounds reasonable to me. Glad you sorted that out. – TheEnigma2112 Feb 01 '13 at 01:22
1

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.

Cocoanetics
  • 8,171
  • 2
  • 30
  • 57
0

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.

rdelmar
  • 103,982
  • 12
  • 207
  • 218
  • How would i go back to the tab view though? As I'm using a tab application which xcode setup when i created the project – user577732 Jan 12 '13 at 06:58
  • @user577732, I have no idea. You didn't mention that you were using a tab based application. You need to edit your question and fully describe you app structure. – rdelmar Jan 12 '13 at 07:00