Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dismiss pushed view from within Navigation Controller

I have a Navigation Controller with a View Controller displaying a button. The button is linked to another View Controller using a push segue which automatically adds a top navigation bar with a back button. This all works fine. Pressing the back button slides off the 2nd view and returns to the 1st.

I have a button on the 2nd View Controller, that when pressed runs some code and a delegate call back to the 1st View Controller. Again this works fine.

Now I just need to dismiss the 2nd pushed View from code as if the back button was pressed. I have tried using dismissModalViewCcontrollerAnimated and dismissViewControllerAnimated, however they both dismiss the whole Navigation Controller which removes view 2 and 1 (returning bak to my main menu).

Whats the correct way to slide off the view.

like image 792
Darren Avatar asked Feb 25 '12 14:02

Darren


People also ask

How do you dismiss a pushed view controller in Swift?

If you are using pushviewcontroller method then to dismiss you have to use popviewcontroller method .

How do you dismiss the presenting view controller?

When it comes time to dismiss a presented view controller, the preferred approach is to let the presenting view controller dismiss it. In other words, whenever possible, the same view controller that presented the view controller should also take responsibility for dismissing it.

How do I remove a view controller from navigation stack?

Use this code and enjoy: NSMutableArray *navigationArray = [[NSMutableArray alloc] initWithArray: self. navigationController. viewControllers]; // [navigationArray removeAllObjects]; // This is just for remove all view controller from navigation stack.


5 Answers

Obtain a reference to your UINavigationController and call

- (UIViewController *)popViewControllerAnimated:(BOOL)animated

on it.

like image 80
Stavash Avatar answered Sep 27 '22 11:09

Stavash


In Swift it would be calling the method

navigationController?.popViewController(animated: true)
like image 40
Garrett Cox Avatar answered Sep 27 '22 11:09

Garrett Cox


If we use push segue, then use popViewController

@IBAction func backButtonClicked(_ sender: Any) {
    self.navigationController?.popViewController(animated: false)
}
like image 31
Alvin George Avatar answered Sep 27 '22 11:09

Alvin George


In swift you can also call:

self.navigationController?.popToRootViewControllerAnimated(true)
like image 30
jnwagstaff Avatar answered Sep 29 '22 11:09

jnwagstaff


On Objective-C is

[self.navigationController popViewControllerAnimated:YES];

for a jump to the first root controller

[self.navigationController popToRootViewControllerAnimated:YES];

or is a possible move to the specific controller

[self.navigationController popToViewController:(nonnull UIViewController *) animated:(BOOL)];

animation specific animation process of move the controller. If the animation is false the controller will appear without animations. The UIViewController must be from one which is on the stack.

like image 34
Jan Damek Avatar answered Sep 27 '22 11:09

Jan Damek