Experience-Parent and Child controllers (verify Apple's sentence)
Apple has a very good official saying:When the view of the controller is parent-child, it is best for the Controller to be parent-child.
In my previous article, I mentioned a very serious problem in the display of the controller view, that is, when the control view is still in progress, but the controller is not there, the data cannot be displayed, so we need to find a way to keep the Controller's life. Let's continue to look at how to keep the Controller's life.
Today, we will use the case of screen rotation to illustrate a problem: when the view of the controller is in a parent-child relationship, what will happen when the controller is not in a parent-child relationship. Let's look at a case study. In ipad development, screen rotation often happens because the screen is large.
@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(10, 70, 300, 300); } return _one;}- (void)viewDidLoad{ [super viewDidLoad];}- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration{ NSLog(@"willRotateToInterfaceOrientation");}- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation{ NSLog(@"didRotateFromInterfaceOrientation");}- (IBAction)vc1 { [self.view addSubview:self.one.view];}
Here, we first load a ZYOneViewController Through laziness, and then use a property to strongly reference him to protect his life. Then we listen to the screen rotation event of ZYViewController. Next we will listen to the screen rotation event in 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 let's take a look at the printed screen rotation and the printed results:
Obviously, the ZYOneViewController control is unknown when ZYViewController controls rotation. Because their controllers are not in the parent-child relationship, they are not in the parent-child relationship. If the ZYViewController controller rotates, tell the ZYOneViewController controller, right.
Now let's turn them into a parent-child relationship. Then let's take a look at the results:
@implementation 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 let's take a look at the rotating results:
So remember,When the view of the controller is parent-child, it is best for the Controller to be parent-child.