ViewController 生命周期 UINavigationController
我有一组 UIViewControllers,显示在 UINavigationController 的根视图控制器中...我推入堆栈的一个控制器有问题。每次 viewDidLoad 时它都需要呈现一个 UIImagePickerController,但是仅在第一次推送时才会这样做。为了解决这个问题,我在根视图控制器中实现了 UINavigationControllerDelegate:
- (void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated {
[viewController viewDidAppear:animated];
}
- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated {
[viewController viewWillAppear:animated];
}
问题是它在第一次推送时调用这些消息两次......否则会很棒。我缺少什么?
I have an array of UIViewControllers that I display in my Root View Controller of a UINavigationController... I have an issue with one of my controllers that I push on the stack. It needs to present a UIImagePickerController each time viewDidLoad, however only does so the first push. To get around this I implement UINavigationControllerDelegate in my Root View Controller:
- (void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated {
[viewController viewDidAppear:animated];
}
- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated {
[viewController viewWillAppear:animated];
}
The problem is it calls these messages twice the first push... otherwise would be great. What am I missing?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可能想在 viewDidAppear: 而不是 viewDidLoad: 中显示 UIImagePickerController。由于您将视图控制器保存在数组中,因此它们仅实例化一次,因此 viewDidLoad: 很可能仅在每个视图控制器上调用一次。 viewDidAppear:每次推送或呈现视图控制器时都会调用。
如果您的应用程序陷入内存不足的情况,则可能会在某个时刻调用 viewDidUnload: ,从而导致 viewDidLoad: 在稍后某个时刻再次被调用,但您根本不能依赖于此。
You probably want to show the UIImagePickerController in viewDidAppear: instead of viewDidLoad:. Since you are keeping your view controllers in an array, they are only instantiated once therefore viewDidLoad: will most likely only be called once on each view controller. viewDidAppear: will be called every time the view controller is pushed or presented.
If your app got into a low memory situation it is possible that viewDidUnload: would be called at some point leading to viewDidLoad: being called again at some later point but you cannot rely on this at all.