简体   繁体   English

在调用popToRootViewControllerAnimated:YES后在self.navigationController中取消分配或删除ViewControllers

[英]dealloc or remove ViewControllers in self.navigationController after popToRootViewControllerAnimated:YES is called

I am using a logout button that calls the 我正在使用一个注销按钮,该按钮

[self.navigationController popToRootViewControllerAnimated:YES];

method. 方法。 After this is called I want to dealloc or remove the previous ViewControllers stored in the 'stack' of self.navigationController.viewControllers 调用此方法后,我想取消分配或删除存储在self.navigationController.viewControllers的“堆栈”中的以前的ViewControllers

I have tried a couple of ways to delete the 'stack', one by: 我尝试了几种删除“堆栈”的方法,一种方法是:

NSMutableArray *stack = [[NSMutableArray alloc] initWithArray:self.navigationController.viewControllers];
[stack removeAllObjects];
self.navigationController.viewControllers = stack;

so far nothing I've done deletes the stack. 到目前为止,我没有做过的任何事情都会删除堆栈。 And I know they are not being deleted because I test it by 而且我知道它们不会被删除,因为我通过

NSLog(@"%d", (int)self.navigationController.viewControllers.count);
[self.navigationController popToViewController:self.navigationController.viewControllers[2];

and it still pops to the ViewController. 并且仍然弹出到ViewController。

any ideas? 有任何想法吗?


Update: Logic in the calls 更新:通话逻辑

ViewController that calls the Pop method 调用Pop方法的ViewController

[self.navigationController popToRootViewControllerAnimated:YES];

RootViewController RootViewController的

NSLog(@"%d", (int)self.navigationController.viewControllers.count);
[self.navigationController popToViewController:self.navigationController.viewControllers[2];

Your stack would have been removed as soon as you call popViewController: to check try this 只要调用popViewController,您的堆栈就会被删除:

[self.navigationController popToViewController:self.navigationController.viewControllers[2];
 double delayInSeconds = .1;
    dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayInSeconds * NSEC_PER_SEC));
    dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
NSLog(@"%d", (int)self.navigationController.viewControllers.count);
    });

instead of 代替

NSLog(@"%d", (int)self.navigationController.viewControllers.count);
[self.navigationController popToViewController:self.navigationController.viewControllers[2];

You should log view controllers in the stack after you call popToViewController: method (In the code you provided you have NSLog method before you call popToViewController: method. Just try this code and see that "BEFORE POP" is larger than "AFTER POP". 在调用popToViewController:方法之后,应该在堆栈中记录视图控制器(在您提供的代码中, 调用popToViewController:方法之前,您已经拥有NSLog方法。只需尝试以下代码,看看“ BEFORE POP”大于“ AFTER POP”。

NSLog(@"BEFORE POP: %d", (int)self.navigationController.viewControllers.count);
[self.navigationController popToRootViewControllerAnimated:YES];
NSLog(@"AFTER POP: %d", (int)self.navigationController.viewControllers.count);

popToViewController method will remove all viewController s which were added to the stack after the viewController you pass as a parameter. popToViewController方法将删除您作为参数传递的viewController之后添加到堆栈中的所有viewController And it does not matter if you pass YES in popToRootViewControllerAnimated: , the stack will be updated right after you call this method before the animation is finished. 而且,如果在popToRootViewControllerAnimated:传递YES也没有关系,则在动画完成之前调用此方法后,堆栈将立即更新。 Although the actual view controller object will probably be still retained until the animation is finished, but that's UINavigationController 's job, so do not worry about that. 尽管实际的视图控制器对象可能仍会保留到动画完成为止,但这是UINavigationController的工作,因此不必担心。

[self.navigationController popToRootViewControllerAnimated:YES];

will update the stack contents after the pop animation is complete. 弹出动画完成后,将更新堆栈内容。

So assuming you have a hierarchy like this: 因此,假设您具有这样的层次结构:

NavigationController RootViewController FirstViewController SecondViewController NavigationController RootViewController FirstViewController SecondViewController

your stack count should be 3 您的筹码数量应为3

if you call [self.navigationController popToRootViewControllerAnimated:YES]; 如果调用[self.navigationController popToRootViewControllerAnimated:YES]; within any of the view controllers, at the end of the animation, your hierarchy should look like this: 在任何视图控制器中,在动画的结尾,您的层次结构应如下所示:

NavigationController RootViewController NavigationController RootViewController

and you stack count should be 1 并且您的堆栈数应该为1

If this isn't the case, then perhaps you are somehow corrupting the stack in code we haven't seen yet. 如果不是这种情况,则可能是您以某种方式破坏了我们尚未看到的代码中的堆栈。

Are you using ARC? 您在使用ARC吗? If so, the popped view controllers will be automatically deallocated. 如果是这样,弹出的视图控制器将被自动释放。 If they are not deallocated then there are some variables which are retained by ARC. 如果未释放它们,则ARC将保留一些变量。 Take a look at your code and see if you have some strong references declared inside the view controllers. 查看您的代码,查看是否在视图控制器中声明了一些强引用。

For example, properties which are not IBOutlets causes ARC to retain them even when the view controllers are popped out from the navigation controller, and dealloc is never called on these view controllers. 例如,即使从导航控制器弹出视图控制器,但不是IBOutlet的属性也会导致ARC保留它们,并且永远不会在这些视图控制器上调用dealloc。 Transform these properties into private variables and then use public methods to access them. 将这些属性转换为私有变量,然后使用公共方法访问它们。

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

相关问题 在[self.navigationController popToRootViewControllerAnimated:YES]期间分配变量; - Assigning a variable during [self.navigationController popToRootViewControllerAnimated:YES]; self.navigationController 分配为 RootViewController 后是 null? - self.navigationController is null after it is assign as RootViewController? [self.navigationController popViewControllerAnimated:YES]; 在委托回调中不起作用 - [ self.navigationController popViewControllerAnimated:YES ]; doesn't work in delegate callback self.navigationcontroller popviewcontrolleranimated是,终止我的应用 - self.navigationcontroller popviewcontrolleranimated yes, terminates my app [self.navigationController pushViewController:ngView animated:YES]; 不工作 - [self.navigationController pushViewController:ngView animated:YES]; not working iOS-[self.navigationController setToolbarHidden:YES动画:NO]崩溃 - iOS - Crash on [self.navigationController setToolbarHidden:YES animated:NO] Deallocated error self.navigationController ==无 - self.navigationController == nil 即使嵌入导航控件中,self.navigationController也为“ null” - self.navigationController is 'null' even after embed in a navigationcontroller Swift - self.navigationController在转换后变为nil - Swift – self.navigationController becomes nil after transition Self.NavigationController =在表视图中为null? - Self.NavigationController = null in tableview?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM