Cat Share, must boutique
Original articles, welcome reprint. Reprint Please specify: Sanayu's Blog
Address: http://blog.csdn.net/u013357243
One: Effect
Two: Project code
This demo with a few controllers draw a xib, casually dragged a few space, mainly a few buttons to switch, the main code show under:
////NYVIEWCONTROLLER.M//display of the controller's view////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;@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 (Ten, -, -, -); }return_one;} -(Nytwoviewcontroller *) two{if(!_two) { Self. the= [[Nytwoviewcontroller alloc] init]; Self. the. View. Frame= CGRectMake (Ten, -, -, -); }return_two;} -(Nythreeviewcontroller *) three{if(!_three) { Self. Three= [[Nythreeviewcontroller alloc] init]; Self. Three. View. Frame= CGRectMake (Ten, -, -, -); }return_three;}/** * Called when it is about to rotate to a screen * /- (void) Willrotatetointerfaceorientation: (uiinterfaceorientation) Tointerfaceorientation Duration: (Nstimeinterval) duration{NSLog(@"Nyviewcontroller---willrotatetointerfaceorientation");} - (void) Didrotatefrominterfaceorientation: (uiinterfaceorientation) frominterfaceorientation{NSLog(@"Nyviewcontroller---didrotatefrominterfaceorientation");} - (void) viewdidload{[SuperViewdidload];//Nytestviewcontroller *test = [[Nytestviewcontroller alloc] init];//Test.view.frame = CGRectMake ( +, +, +);//Test.view.backgroundColor = [Uicolor redcolor];//[Self.view AddSubview:test.view];//self.test = test; //If found: The controller's view is still in, but the data above the view is not displayed, it is probably because: the controller was destroyed in advance //1. The view of a controller can be arbitrarily adjusted to the size and position of the //2. A view of a controller can be added to another view at will //3. If you add a controller's view to another view, try to ensure that the controller is not destroyed //4. Principle: As long as the view is in, the controller of the view must be in order to ensure that the data and business logic inside the view is normal}- (ibaction) VC1 {[ Self. the. ViewRemovefromsuperview]; [ Self. Three. ViewRemovefromsuperview]; [ Self. ViewAddsubview: Self. One. View];} - (ibaction) VC2 {[ Self. One. ViewRemovefromsuperview]; [ Self. Three. ViewRemovefromsuperview]; [ Self. ViewAddsubview: Self. the. View];} - (ibaction) VC3 {[ Self. the. ViewRemovefromsuperview]; [ Self. One. ViewRemovefromsuperview]; [ Self. ViewAddsubview: Self. Three. View];}@end
Three: Rotating event issues
This may seem like a lot of demand, but sometimes we find some problems, such as when the screen spins and the event can't be delivered.
/** * 即将旋转到某个屏幕时调用 */- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration{ NSLog(@"NYViewController---willRotateToInterfaceOrientation");}- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation{ NSLog(@"NYViewController---didRotateFromInterfaceOrientation");}
If we write these two methods to the three controller of one and three, the corresponding screen rotation, only the main controller print this method, but the other controller is not, the reason is that their controller is peer, although the view is a parent-child relationship, The solution is to set up a parent-child relationship for the controller.
Four: Solve the Code
When the controller's view is a parent-child relationship, the controller is best for parent-child relationships.
NYOneViewController *one = [[NYOneViewController alloc]init];
Make one controller a child of the current self (hwviewcontroller)
[self addChildViewController:one];
Adding a sub-controller by Addchildviewcontroller, the controller will be placed in the childviewcontrollers array
As long as self is in, the childviewcontrollers array is in the array and the sub-controller is
////NYVIEWCONTROLLER.M//display of the controller's view////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 /** * Called when it is about to rotate to a screen * /- (void) Willrotatetointerfaceorientation: (uiinterfaceorientation) Tointerfaceorientation Duration: (Nstimeinterval) duration{NSLog(@"Nyviewcontroller---willrotatetointerfaceorientation");} - (void) Didrotatefrominterfaceorientation: (uiinterfaceorientation) frominterfaceorientation{NSLog(@"Nyviewcontroller---didrotatefrominterfaceorientation");} - (void) viewdidload{[SuperViewdidload];//When the controller's view is a parent-child relationship, then the controller is best for parent-child relationshipsNyoneviewcontroller *one = [[Nyoneviewcontroller alloc]init];//Make one controller a child of the current self (hwviewcontroller)[ SelfAddchildviewcontroller:one];//Add a sub-controller by Addchildviewcontroller, then this controller will be placed in the childviewcontrollers array //As long as self is in, the childviewcontrollers array is in the array and the sub-controller isNytwoviewcontroller *two = [[Nytwoviewcontroller alloc]init]; [ SelfAddchildviewcontroller:two]; Nythreeviewcontroller *three = [[Nythreeviewcontroller alloc]init]; [ SelfAddchildviewcontroller:three];} - (ibaction) VC1 {Nyoneviewcontroller *one = Self. Childviewcontrollers[0]; Nytwoviewcontroller *two = Self. Childviewcontrollers[1]; Nythreeviewcontroller *three = Self. Childviewcontrollers[2]; [Both. ViewRemovefromsuperview]; [Three. ViewRemovefromsuperview]; [ Self. ViewAddsubview:one. View];} - (ibaction) VC2 {Nyoneviewcontroller *one = Self. Childviewcontrollers[0]; Nytwoviewcontroller *two = Self. Childviewcontrollers[1]; Nythreeviewcontroller *three = Self. Childviewcontrollers[2]; [One. ViewRemovefromsuperview]; [Three. ViewRemovefromsuperview]; [ Self. ViewAddsubview:two. View];} - (ibaction) VC3 {Nyoneviewcontroller *one = Self. Childviewcontrollers[0]; Nytwoviewcontroller *two = Self. Childviewcontrollers[1]; Nythreeviewcontroller *three = Self. Childviewcontrollers[2]; [Both. ViewRemovefromsuperview]; [One. ViewRemovefromsuperview]; [ Self. ViewAddsubview:three. View];}@end
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Cat Learn iOS controller view Show parent-child relationship of view and controller parent-Child Relationship _ Resolve screen rotation cannot pass event issues