viewDidAppear called BEFORE it actually appears?
I know that this question sounds similar to this, but I am already aware of the work around. This is more of an analysis question.
The above link is a question specific to UITabBarController
, but I also observed the behavior with UINavigationController
, when I pushViewController:... animated:NO
, ie. no animation.
So my question is WHY does view appears AFTER the viewDidAppear
method, while its very name implies, and definition says, that view had already appeared (definition says view has beed added to the window).
I had marked a breakpoint in the method to see the call stack and here is the log for pushing to UINavigationController
:
- (void)viewDidAppear:(BOOL)animated {
[super viewDidApear:animated];
NSLog("..."); // <<<<<Breakpoint here
}
Here is the log:
(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 -[UI开发者_Go百科NavigationController _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.
I found the following:
ViewWillAppear: I use ViewWillAppear usually just to update the data on the form. So, for the example above, I would use this to actually load the data from my domain into the form. Creation of UIViews is fairly expensive, and you should avoid as much as possible doing that on the ViewWillAppear method, becuase when this gets called, it means that the iPhone is already ready to show the UIView to the user, and anything heavy you do here will impact performance in a very visible manner (like animations being delayed, etc).
ViewDidAppear: Finally, I use the ViewDidAppear to start off new threads to things that would take a long time to execute, like for example doing a webservice call to get extra data for the form above.The good thing is that because the view already exists and is being displayed to the user, you can show a nice "Waiting" message to the user while you get the data
Stolen from the SO-question: What is the difference between -viewWillAppear: and -viewDidAppear:?
精彩评论