【问题标题】:viewDidAppear called BEFORE it actually appears?viewDidAppear 在它实际出现之前调用?
【发布时间】:2011-06-14 09:20:15
【问题描述】:

我知道这个问题听起来类似于this,但我已经知道解决方法。这更像是一个分析问题。

上面的链接是UITabBarController特有的问题,但我也观察到UINavigationController的行为,当我pushViewController:... animated:NO时,即。没有动画。

所以我的问题是为什么视图会出现在viewDidAppear 方法之后,而它的名字暗示,并且定义说,该视图已经出现(定义说视图已经添加到窗口中)。

我在方法中标记了一个断点来查看调用堆栈,这里是推送到UINavigationController的日志:

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidApear:animated];
    NSLog("...");              // <<<<<Breakpoint here
}

这是日志:

(gdb) next
Single stepping until exit from function -[UINavigationController navigationTransitionView:didEndTransition:fromView:toView:], which has no line number information.
(gdb) next
Single stepping until exit from function -[UINavigationTransitionView _notifyDelegateTransitionDidStopWithContext:], which has no line number information.
(gdb) next
Single stepping until exit from function -[UINavigationTransitionView _navigationTransitionDidStop], which has no line number information.
(gdb) next
Single stepping until exit from function -[UIViewAnimationState sendDelegateAnimationDidStop:finished:], which has no line number information.
(gdb) next
Single stepping until exit from function +[UIViewAnimationState popAnimationState], which has no line number information.
(gdb) next
Single stepping until exit from function dyld_stub_objc_msgSend, which has no line number information.
(gdb) next
Single stepping until exit from function objc_msgSend, which has no line number information.
(gdb) next
Single stepping until exit from function -[NSObject(NSObject) release], which has no line number information.
(gdb) next
Single stepping until exit from function dyld_stub_objc_msgSend, which has no line number information.
(gdb) next
Single stepping until exit from function objc_msgSend, which has no line number information.
(gdb) next
Single stepping until exit from function -[UIViewAnimationState dealloc], which has no line number information.
(gdb) next
Single stepping until exit from function -[UINavigationTransitionView transition:fromView:toView:], which has no line number information.
(gdb) next
Single stepping until exit from function -[UINavigationTransitionView transition:toView:], which has no line number information.
(gdb) next
Single stepping until exit from function -[UINavigationController _startDeferredTransitionIfNeeded], which has no line number information.
(gdb) next
Single stepping until exit from function -[UILayoutContainerView layoutSubviews], which has no line number information.
(gdb) next
Single stepping until exit from function -[CALayer layoutSublayers], which has no line number information.
(gdb) next
Single stepping until exit from function dyld_stub_objc_msgSend, which has no line number information.
(gdb) next
Single stepping until exit from function objc_msgSend, which has no line number information.
(gdb) next
Single stepping until exit from function -[NSObject(NSObject) release], which has no line number information.
(gdb) next
Single stepping until exit from function CALayerLayoutIfNeeded, which has no line number information.

【问题讨论】:

    标签: iphone objective-c viewdidappear


    【解决方案1】:

    我发现了以下内容:

    • ViewWillAppear:我使用 ViewWillAppear 通常只是为了更新表单上的数据。因此,对于上面的示例,我将使用它来实际将域中的数据加载到表单中。 UIViews 的创建是相当昂贵的,你应该尽可能避免在 ViewWillAppear 方法上这样做,因为当它被调用时,这意味着 iPhone 已经准备好向用户显示 UIView,你在这里做的任何事情将以非常明显的方式影响性能(如动画延迟等)。

    • ViewDidAppear:最后,我使用 ViewDidAppear 启动新线程来处理需要很长时间才能执行的事情,例如执行 Web 服务调用以获取上述表单的额外数据。好处是因为视图已经存在并且正在向用户显示,所以您可以在获取数据时向用户显示一个很好的“等待”消息

    从 SO 问题中窃取:What is the difference between -viewWillAppear: and -viewDidAppear:?

    【讨论】:

    • 感谢 Joetjah 的回复,但这与我的问题有什么关系?
    • 视图在 viewDidAppear 之后立即显示,但其中的内容尚未加载到屏幕中。根据其他帖子,您可以认为 viewDidAppear 是 viewDidAppear 中定义的操作的加载器,同时正在输出视图。换句话说,不必在出现时显示,但必须从一开始就计算出来的东西。想象一下,出于某种原因,您决定让视图的出现通过一个时间为 3 秒的动画发生。那将是 3 秒的“浪费”,但您可以同时通过 viewDidAppear 加载内容。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-17
    • 1970-01-01
    相关资源
    最近更新 更多