The parent-child relationship of the view and the parent-child relationship of the controller are displayed in the controller view of iOS.

Source: Internet
Author: User

The parent-child relationship of the view and the parent-child relationship of the controller are displayed in the controller view of iOS.

 

I. Effect

Ii. project code

The Demo uses several controllers to draw different xib and drag a few spaces at will, mainly switching between several buttons. The main code is as follows:

/// NYViewController. m // view display of the controller /// Created by apple on 14-10-10. // Copyright (c) 2014 heima. all rights reserved. // # import NYViewController. h # import NYTestViewController. h # import NYOneViewController. h # import NYTwoViewController. h # import NYThreeViewController. h @ interface NYViewController ()-(IBAction) vc1;-(IBAction) vc2;-(IBAction) vc3; @ property (nonatomic, strong) NYTestViewController * tes T; @ property (nonatomic, strong) NYOneViewController * one; @ property (nonatomic, strong) NYTwoViewController * two; @ property (nonatomic, strong) NYThreeViewController * three; @ end @ implementation NYViewController-(NYOneViewController *) one {if (! _ One) {self. one = [[NYOneViewController alloc] init]; self. one. view. frame = CGRectMake (10, 70,300,300);} return _ one;}-(NYTwoViewController *) two {if (! _ Two) {self. two = [[NYTwoViewController alloc] init]; self. two. view. frame = CGRectMake (10, 70,300,300);} return _ two;}-(NYThreeViewController *) three {if (! _ Three) {self. three = [[NYThreeViewController alloc] init]; self. three. view. frame = CGRectMake (10, 70,300,300);} return _ three;}/*** call when you are about to rotate to a screen */-(void) willRotateToInterfaceOrientation :( UIInterfaceOrientation) toInterfaceOrientation duration :( NSTimeInterval) duration {NSLog (@ NYViewController --- timeout);}-(void) duration :( UIInterfaceOrientation) fromInterfaceOrientation {NSLog (@ NYViewController --- timeout);}-(void) viewDidLoad {[super viewDidLoad]; // NYTestViewController * test = [[NYTestViewController alloc] init]; // test. view. frame = CGRectMake (100,100,200,300); // test. view. backgroundColor = [UIColor redColor]; // [self. view addSubview: test. view]; // self. test = test; // if you find that the Controller's view is still present, but the data on the view is not displayed, it is very likely that the controller has been destroyed in advance. // 1. the view of a controller can adjust the size and position at will. // 2. the view of a controller can be added to other views at will. // 3. if you add the view of a controller to other views for display, you need to ensure that the controller is not destroyed. // 4. principle: as long as the view is in, the view controller must be in, in order to ensure that the data and business logic inside the view are normal}-(IBAction) vc1 {[self. two. view removeFromSuperview]; [self. three. view removeFromSuperview]; [self. view addSubview: self. one. view];}-(IBAction) vc2 {[self. one. view removeFromSuperview]; [self. three. view removeFromSuperview]; [self. view addSubview: self. two. view];}-(IBAction) vc3 {[self. two. view removeFromSuperview]; [self. one. view removeFromSuperview]; [self. view addSubview: self. three. view];} @ end
Iii. Rotation events

In this way, it seems that most of the requirements can be fulfilled, but sometimes we will find some problems, such as the event cannot be passed when the screen rotates.

/*** Call **/-(void) rotate :( UIInterfaceOrientation) toInterfaceOrientation duration :( NSTimeInterval) duration {NSLog (@ NYViewController --- willRotateToInterfaceOrientation) when it is about to rotate to a screen );} -(void) didRotateFromInterfaceOrientation :( UIInterfaceOrientation) fromInterfaceOrientation {NSLog (@ NYViewController --- didRotateFromInterfaceOrientation );}

If we write the two methods to the one two three controllers, only the master controller prints the method when the screen rotates, but not the other controllers, the reason for this is that their controllers are level-1. Although view is a parent-child relationship, the solution is to set the parent-child relationship of the controller.

4. Solution code

When the view of the controller is parent-child, it is best for the Controller to be parent-child.

  NYOneViewController *one = [[NYOneViewController alloc]init];

Make one controller a sub-controller of the current self (HWViewController)

    [self addChildViewController:one];

Add a subcontroller by turning off addChildViewController, and the Controller will be placed in the childViewControllers array.
As long as the self is in, the childViewControllers array is in

/// NYViewController. m // view display of the controller /// Created by apple on 14-10-10. // Copyright (c) 2014 heima. all rights reserved. // # import NYViewController. h # import NYTestViewController. h # import NYOneViewController. h # import NYTwoViewController. h # import NYThreeViewController. h @ interface NYViewController ()-(IBAction) vc1;-(IBAction) vc2;-(IBAction) vc3; @ property (nonatomic, strong) NYTestViewController * test; @ end @ implementation NYViewController/*** is called when it is about to rotate to a screen */-(void) willRotateToInterfaceOrientation :( UIInterfaceOrientation) toInterfaceOrientation duration :( NSTimeInterval) duration {NSLog (@ NYViewController --- willRotateToInterfaceOrientation);}-(void) annotation :( UIInterfaceOrientation) fromInterfaceOrientation {NSLog (@ NYViewController --- annotation);}-(void) viewDidLoad {[super viewDidLoad]; // when the Controller's view is in a parent-child relationship, it is best for the Controller to have a parent-child relationship with NYOneViewController * one = [[NYOneViewController alloc] init]; // make one controller the subcontroller of the current self (HWViewController) [self addChildViewController: one]; // Add a subcontroller by turning off addChildViewController, then this controller will be placed in the childViewControllers array // As long as self is in, the childViewControllers array will be in the array, and the subcontroller will be in NYTwoViewController * two = [[NYTwoViewController alloc] init]; [self addChildViewController: two]; NYThreeViewController * three = [[NYThreeViewController alloc] init]; [self addChildViewController: three];}-(IBAction) vc1 {NYOneViewController * one = self. childViewControllers [0]; NYTwoViewController * two = self. childViewControllers [1]; NYThreeViewController * three = self. childViewControllers [2]; [two. view removeFromSuperview]; [three. view removeFromSuperview]; [self. view addSubview: one. view];}-(IBAction) vc2 {NYOneViewController * one = self. childViewControllers [0]; NYTwoViewController * two = self. childViewControllers [1]; NYThreeViewController * three = self. childViewControllers [2]; [one. view removeFromSuperview]; [three. view removeFromSuperview]; [self. view addSubview: two. view];}-(IBAction) vc3 {NYOneViewController * one = self. childViewControllers [0]; NYTwoViewController * two = self. childViewControllers [1]; NYThreeViewController * three = self. childViewControllers [2]; [two. view removeFromSuperview]; [one. view removeFromSuperview]; [self. view addSubview: three. view];} @ end

 

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.