ViewController 生命周期 UINavigationController

发布于 2024-11-17 12:58:32 字数 688 浏览 2 评论 0原文

我有一组 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

落在眉间の轻吻 2024-11-24 12:58:32

您可能想在 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文