Experience-Parent and Child controllers (verify Apple's sentence)

Source: Internet
Author: User

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.