Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using pushViewController after presentModalViewController

In my app I have BaseViewController (NavigationController) as the root controller. I normally use the following code to navigate:

[self.navigationController pushViewController:childController animated:YES];

But on one of the actions I want the next view to animate buttom to top, so I use:

[self presentModalViewController:childController animated:YES];

Everything works so far. In the modal view I then want to push another controller, but this doesn't seem to work. I have tried the following:

// self.navigationController is null, so this doesn't work
[self.navigationController pushViewController:childController animated:YES];

// self.parentViewController is the BaseViewController and not null, but this 
// won't work either. This also generates a warning "UIViewController' may not 
// respond to '-pushViewController:animated:"
[self.parentViewController pushViewController:childController animated:YES];

In both cases nothing happens. Is pushViewController disabled while a modal view is still showing? If so, is there another way I can:

  1. Animate next controller from bottom to top
  2. Animate the next controller from left to right, with a back button as usual. The back button should take you back to the previous (modal) view.

?

like image 226
Mads Mobæk Avatar asked Mar 10 '10 16:03

Mads Mobæk


People also ask

How do I push a presented ViewController?

VC3 -> present VC2 -> VC1 VC2 needs to be in a UINavigationController then after you present it you can push VC3. The back button will work as expected on VC3, for VC2 you should call dismiss when the back button is pressed. Try implementing some of that in code and then update your question.


2 Answers

If you presentModelViewController then you need to dismiss it before you can call the navigation controller's methods, otherwise you have to put this view controller into the navigation stack in order to put another view controller on top of it.

like image 130
Hoang Pham Avatar answered Sep 29 '22 19:09

Hoang Pham


Use the following lines of code to present childController, then navigationcontroller will not be null so it will work as u want.

UINavigationController * childNavigationController = [[UINavigationController alloc] initWithRootViewController:childController];
childNavigationController.navigationBarHidden = YES;//if u want to show navigation bar then remove this line
[self.navigationController presentModalViewController:coinsNavigationController animated:YES];

After presentModelViewController you can present or push any other view controller within presented view controller.

// self.navigationController in this case will never be null. so following line will work perfectly inside presented child View controller (childController)
[self.navigationController pushViewController:childController animated:YES];

Here is the desired output:

  1. childController will be presented via animation bottom to top over BaseViewController
  2. An other instance of child controller will be pushed via animation left to right over childController.

Hope this will be useful and easy solution.

like image 35
W.S Avatar answered Sep 29 '22 20:09

W.S