Apple's official words are very good: when the controller's view is a parent-child relationship, then the controller is best for parent-child relationships
I have a blog before that the controller view display inside I said a very serious problem, is when the control of the view is still in, but the controller is not, causing the data can not display the problem, so we have to find ways to keep the controller's life. So let's continue today to see how to keep the controller alive.
- Today we are going to use a case of screen rotation to illustrate a problem: when the controller's view is a parent-child relationship, the controller is not a parent-child relationship, there is a serious problem.
- Look at a case, in the ipad development, screen rotation is often what happens, because the screen is larger.
@interface zyviewcontroller ()- (ibaction) VC1;@property(nonatomic,Strong) Zyoneviewcontroller *one;@end @implementation zyviewcontroller -(Zyoneviewcontroller *) one{if(!_one) { Self. One= [[Zyoneviewcontroller alloc] init]; Self. One. View. Frame= CGRectMake (Ten, -, -, -); }return_one;} - (void) viewdidload{[SuperViewdidload];} - (void) Willrotatetointerfaceorientation: (uiinterfaceorientation) Tointerfaceorientation Duration: (Nstimeinterval) duration{NSLog(@"Willrotatetointerfaceorientation");} - (void) Didrotatefrominterfaceorientation: (uiinterfaceorientation) frominterfaceorientation{NSLog(@"Didrotatefrominterfaceorientation");} - (ibaction) VC1 {[ Self. ViewAddsubview: Self. One. View];}
Here, we first load a zyoneviewcontroller by lazy, and then use a property to make a strong reference to him, to protect his life. We then monitor the Zyviewcontroller screen rotation event. Next we also listen to the screen rotation event in the Zyoneviewcontroller:
@implementation zyoneviewcontroller -(void ) Willrotatetointerfaceorientation: (uiinterfaceorientation ) Tointerfaceorientation Duration: (nstimeinterval ) duration{nslog (@" zyoneviewcontroller--willrotatetointerfaceorientation " );} -(void ) Didrotatefrominterfaceorientation: ( Uiinterfaceorientation ) frominterfaceorientation{nslog (@" zyoneviewcontroller--didrotatefrominterfaceorientation ");} -(ibaction ) Onebtnclick {nslog (@ "Onebtnclick" );}
Then we look at the printed screen rotation, printing the result:
It is obvious that the Zyoneviewcontroller control does not know when the Zyviewcontroller controls the rotation. Because of their controller, not father-son relationship, then not the father-child relationship, Zyviewcontroller controller rotation, with what to tell the Zyoneviewcontroller controller, right.
Now let's make them a father and son relationship and look at the results:
@implementati On zyviewcontroller -(Zyoneviewcontroller *) one{if (!_one) {self .one = [ [Zyoneviewcontroller alloc] init]; self .one .view .frame = CGRectMake (10 , 70 , 300 , 300 ) ; [self addchildviewcontroller:self .one ]; } return _one;}
Then we look at the results of the rotation:
So we have to remember that when the controller's view is a parent-child relationship, the controller is best to be a parent-child relationship .
Experience-parent-Child controller (verify Apple's Word)