Popping and Pushing View Controllers in Same Action

popping and pushing view controllers in same action

 MapsViewController *aViewController = [[MapsViewController alloc]
initWithNibName:@"MapsViewController" bundle:nil];
// locally store the navigation controller since
// self.navigationController will be nil once we are popped
UINavigationController *navController = self.navigationController;

// retain ourselves so that the controller will still exist once it's popped off
[[self retain] autorelease];

// Pop this controller and replace with another
[navController popViewControllerAnimated:NO];//not to see pop

[navController pushViewController:aViewController animated:YES];//to see push or u can change it to not to see.

Or

 MapsViewController *aViewController = [[MapsViewController alloc]
initWithNibName:@"MapsViewController" bundle:nil];

UINavigationController *navController = self.navigationController;

//Get all view controllers in navigation controller currently
NSMutableArray *controllers=[[NSMutableArray alloc] initWithArray:navController.viewControllers] ;

//Remove the last view controller
[controllers removeLastObject];

//set the new set of view controllers
[navController setViewControllers:controllers];

//Push a new view controller
[navController pushViewController:aViewController animated:YES];

How to pop VC to jump previous two View Controllers using Swift Language?

You are popping (going back to previous controller) instead of pushing (adding new) view controller. What you should do is:

@IBAction func actSingUp(_ sender: Any) {

let storyboard = UIStoryboard(name: "LoginSB", bundle: nil)
let signupvc = storyboard.instantiateViewController(withIdentifier: "loginVC") as! LoginVC

// this step is optional, it will remove SignUp controller from navigation stack
navigationController?.popViewController(animated: false)

// present login controller
navigationController?.pushViewController(signupvc, animated: true)
}

How can I close my current view controller and push another view controller in iOS]

Calling push and pop on the self at the same time will not work, as self has already been removed. You need to handle the stack of view controller -

 UINavigationController *navigationController = self.navigationController;

NSMutableArray *activeViewControllers=[[NSMutableArray alloc] initWithArray: navigationController.viewControllers] ;
[activeViewControllers removeLastObject];

// Reset the navigation stack
[navigationController setViewControllers:activeViewControllers];

[navigationController pushViewController:yourViewController animated:YES];

Apart from this if you don't want this view controller , when popping from your new view controller, you have other options available.

Like firstVC -> secondVC -> thirdVC

Now from thirdVC if you directly want to come to firstVC (skipping secondVC, i believe which is why you want both push and pop). Then for this purpose you can use - popToViewController.

[self.navigationController popToViewController:firstVC animated:YES];

Just checked that there are already couple of questions available on the same topic, you can refer those too for good opinion and answers-

How can I pop a view from a UINavigationController and replace it with another in one operation?

popping and pushing view controllers in same action

How to pop a viewController and then Push a viewController through delegation

Pushing and popping multiple view controllers causes random crash

My guess is that you push multiple view controllers with animations - this may be a root cause of error. If you push more than one view controller you should animate only LAST push, say:

VC1 *vc1 = [self.storyboard instantiateViewControllerWithIdentifier:@"VC1"];
[self.navigationController pushViewController:vc1 animated:NO];
VC2 *vc2 = [self.storyboard instantiateViewControllerWithIdentifier:@"VC2"];
[self.navigationController pushViewController:vc1 animated:NO];
VC3 *vc3 = [self.storyboard instantiateViewControllerWithIdentifier:@"VC3"];
[self.navigationController pushViewController:vc1 animated:YES];

However, i hardly imagine the situation where the multiple push would be necessary - i think it always leads to bad UX.

Storyboard - Popping to a View Controller then Pushing Causes multiple pushes

Turns out I had the following issue

Button press causes segue action in storyboard
I did the same segue action in code on the button, hence pushing multiple times

I have now solved this issue

How to pop a viewController and then Push a viewController through delegation

You should use a flag to overcome this situation. You set this flag in viewWillDisappear method of view controller being popped. When this flag is set then and then you can push another view controller on stack. Hope it's clear.



Related Topics



Leave a reply



Submit