简体   繁体   中英

How can I dealloc manually UIViewController from another UIViewController in UINavigationController without import the first UIViewController class?

I've a navigationController. The first viewcontroller is a kind of class FirstViewController. When I tap a button in FirstViewController, it push in navigationController the second viewController that is a kind of class SecondViewController. When I tap a button in SecondViewController, I'd like to dealloc the FirstViewController (previously saved in navigationController) so as to start again as if it was the first time that I open the FirstViewController when I tap back button in navigationItem of SecondViewController. Here the code of the method called when I tap the button in secondViewController:

NSArray * navigationPath = [self.navigationController viewControllers];
UIViewController *previousVC = [navigationPath objectAtIndex:[navigationPath count]-2];
[previousVC performSelector:@selector(viewDidUnload)];

It doesn't dealloc the FirstViewController. There is a way to do it?

Create a new first view controller

FirstViewController *first = [[FirstViewController alloc]init];

Reset the navigation stack

[self.navigationController setViewControllers:@[first, self] animated:NO];

Navigate to first

[self.navigationController popViewController:YES];

You will need to reset the array of view controllers in the navigation controller.

In your second view controller:

[self.navigationController setViewControllers:@[self] animated:NO];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM