UiViewController - 弹出不弹出
当从另一个 UIViewController 按下按钮时,我显示 UIControllerView 子类,如下所示:
- (IBAction)openNextLevelViewController
{
NSLog(@"openNextlevelViewController");
[self.navigationController pushViewController:nextLevelViewController animated:YES];
}
应用程序将从触发此方法的按钮按下时从该视图返回:
-(IBAction) returnToStart {
NSLog(@"returnToStart method called");
[self.navigationController popViewControllerAnimated:YES];
}
问题是显示的视图不会在弹出窗口上被销毁/解除分配。因此,当它被推送时,它不会执行 viewDidLoad,它会启动一些变量。这可能会导致相关问题,即当用户第二次按下返回按钮时,“弹出”不再导致返回到前一个控制器。
处理这个问题的最佳方法是什么?我可以将初始化代码移至“willAppear”方法,但似乎几乎可以随机调用它。
I'm displaying UIControllerView subclass when a button is pressed from a another UIViewController like this:
- (IBAction)openNextLevelViewController
{
NSLog(@"openNextlevelViewController");
[self.navigationController pushViewController:nextLevelViewController animated:YES];
}
And the app will return from that view on a button push which trigger this method:
-(IBAction) returnToStart {
NSLog(@"returnToStart method called");
[self.navigationController popViewControllerAnimated:YES];
}
The problem is that the displayed view not getting destroyed/deallocated on the pop. As a result, when it gets pushed, it's not executing the viewDidLoad, which initiates some variables. This may be causing a related problem where, the second time through, when the user presses the return button, the "pop" no longer causes a return to the previous controller.
What's the best way to deal with this? I could move the initialization code to the "willAppear" method, but it seems as if that could be called almost randomly.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好吧,它没有被释放,因为 nextLevelViewController 仍然被保留在其他地方。最有可能在你的 nextLevelViewController 变量中。
现在
,当您的控制器弹出时,它应该被释放(不应该保留在其他地方)。下次您调用 openNExtLevelViewController 时,它无论如何都会初始化 viewController 的新实例。
我喜欢在不再需要(显示)时释放 viewController,而不是将其保留在内存中。尽可能让 navigationController 和 TabBarController 处理 viewController。
Well, its not getting released because nextLevelViewController is still being retained somewhere else. Most likely in your nextLevelViewController variable.
Further On
Now when your controller gets popped, it SHOULD get released (shouldn't have been retained anywhere else). And next time you call openNExtLevelViewController, it'll be initializing a new instance of your viewController anyway.
I'm a fan of releasing viewController when it is no longer needed (displayed), instead of holding it in memory. Let navigationController and TabBarController handle viewControllers whenever possible.