Today there is a very strange problem, because the Viewdidload method is called, and the Viewdidappear method is not called, resulting in the code in the Viewdidappear method is not executed.
Here's a demo that completely simulates this scenario.
The first is a simple storyboard, where Appdelegate's keywindow is window, andwindow's Rootviewcontroller is a uinavigationcontroller. As follows:
The Rootviewcontroller of Navigationcontroller is Viewcontroller.
The Add button on the Viewcontroller is used to add a childviewcontroller whose class is the Newviewcontroller class.
The Remove button on the Viewcontroller removes the Childviewcontroller from the previous Add.
The following is the code for the Viewcontroller class:
#import "ViewController.h" #import "NewViewController.h" #import "AppDelegate.h" @interface Viewcontroller () @property (Strong, Nonatomic) Newviewcontroller *NVC; @end @implementation viewcontroller-(void) viewdidload{ [Super Viewdidload];} -(Ibaction) AddChild: (ID) Sender { UIWindow *keywindow = [UIApplication Sharedapplication].keywindow; Uiviewcontroller *controller = Keywindow.rootviewcontroller; NSLog (@ "%@", controller); SELF.NVC = [Newviewcontroller new]; [SELF.NVC Willmovetoparentviewcontroller:controller]; [Controller ADDCHILDVIEWCONTROLLER:SELF.NVC]; Self.nvc.view.frame = CGRectMake (+, +, +); [Controller.view AddSubview:self.nvc.view]; [SELF.NVC Didmovetoparentviewcontroller:controller];} -(Ibaction) Remove: (ID) Sender { [Self.nvc.view Removefromsuperview]; [SELF.NVC Removefromparentviewcontroller];}
There is also the code for the Newviewcontroller class:
#import "NewViewController.h" @implementation newviewcontroller-(void) viewdidload{ [Super Viewdidload]; NSLog (@ "Viewdidload"); Self.view.backgroundColor = [Uicolor redcolor];} -(void) Viewdidappear: (BOOL) animated { [Super viewdidappear:animated]; NSLog (@ "Viewdidappear");} @end
Run the demo, click Add, click Remove, click Add, and then click Remove.
The output log is as follows:
2014-09-25 01:35:41.824 parentvcdemo[24025:60b] <uinavigationcontroller:0x8c86aa0>2014-09-25 01:35:41.826 PARENTVCDEMO[24025:60B] viewdidload2014-09-25 01:35:43.232 parentvcdemo[24025:60b] <UINavigationController: 0x8c86aa0>2014-09-25 01:35:43.233 parentvcdemo[24025:60b] Viewdidload
You can see that the rootviewcontroller of Keywindow is a Uinavigationcontroller class, Addchildviewcontroller on the controller, An instance of the Newviewcontroller class.
The Viewdidload method in the obvious newviewcontroller is called, and the Viewdidappear method is not called.
So, if you put the newviewcontroller of the view background color red code into the Viewdidappear method, you will live or die to see a red view appears in the screen.
Cause: Addchildviewcontroller on the Uinavigationcontroller class or its subclass object will cause the Viewdidappear method in the child View controller to not be executed.
Suggestions:
1. Since Newviewcontroller is Removefromsuperview, no object holds it, the object will be released, and the next time add a Newviewcontroller, its Viewdidload method must be called. So instead of worrying about the Viewdidload method calling only once, you can move the code in the Viewdidappear method to execute in the Viewdidload method.
2. Get the correct keywindow, or get the correct controller in the Keywindow, For example, we can determine if Keywindow's Rootviewcontroller is Uinavigationcontroller class or its subclasses, and if so, can get the topviewcontroller of the navigation controller. For example, this:
UIWindow *keywindow = [UIApplication Sharedapplication].keywindow; Uiviewcontroller *controller; if ([Keywindow.rootviewcontroller Iskindofclass:[uinavigationcontroller class]]) { Uinavigationcontroller * Navicontroller = (Uinavigationcontroller *) Keywindow.rootviewcontroller; controller = Navicontroller.topviewcontroller; } else { controller = Keywindow.rootviewcontroller; }
But will uitabbarcontroller be affected? Are there any more pits? I don't know, it's really embarrassing.
It's almost two o'clock, good night. Don't harass some people, haha.
Addchildviewcontroller the Viewdidappear method does not get the calling problem