简体   繁体   English

如何在不导入第一个UIViewController类的情况下从UINavigationController中的另一个UIViewController手动取消分配UIViewController?

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

I've a navigationController. 我有一个navigationController。 The first viewcontroller is a kind of class FirstViewController. 第一个viewcontroller是FirstViewController类。 When I tap a button in FirstViewController, it push in navigationController the second viewController that is a kind of class SecondViewController. 当我在FirstViewController中点击一个按钮时,它将第二个viewController推入navigationController中,该类是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. 当我点击SecondViewController中的按钮时,我想取消分配FirstViewController(以前保存在navigationController中),以便再次启动,就好像这是我第一次点击SecondViewController的NavigationItem中的后退按钮时打开FirstViewController一样。 Here the code of the method called when I tap the button in secondViewController: 这是我在secondViewController中点击按钮时调用的方法的代码:

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

It doesn't dealloc the FirstViewController. 它不会取消分配FirstViewController。 There is a way to do it? 有办法吗?

Create a new first view controller 创建一个新的第一个视图控制器

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

Reset the navigation stack 重置导航堆栈

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

Navigate to first 导航到第一个

[self.navigationController popViewController:YES]; [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];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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