First, the interface, button one, two, three respectively corresponding to the three controller view, click to achieve the switch. Personal feeling father and son Controller is the focus of the controller's view between the establishment of a parent-child relationship, the controller is not established, occurs in the view above the event, the corresponding view may not receive, the controller has established a parent-child relationship, the event can be passed to the appropriate controller.
The practice code is as follows:
1 #import "ViewController.h"2 #import "OneTableViewController.h"3 #import "TwoViewController.h"4 #import "ThreeViewController.h"5 6 @interfaceViewcontroller ()7 /** Curview*/8@property (nonatomic,strong) Uiviewcontroller *CURVC;9 /** Old*/Ten @property (nonatomic,assign) Nsinteger Oldindex; One /** View*/ A@property (nonatomic,strong) UIView *Contentview; - @end - the @implementationViewcontroller - -- (void) Viewdidload { - [Super Viewdidload]; + - //do not give the button, only to the View animation solution-more than a view will be animated view package, animation added to this view +UIView *contentview = [[UIView alloc] Initwithframe:cgrectmake (0, -, Self.view.bounds.size.width, Self.view.bounds.size.height- -)]; A [Self.view Addsubview:contentview]; at -Self.contentview =Contentview; - - [self Addchildviewcontroller:[[onetableviewcontroller alloc] init]]; - [self Addchildviewcontroller:[[twoviewcontroller alloc] init]]; - [self Addchildviewcontroller:[[threeviewcontroller alloc] init]]; in - to } + --(Ibaction) Btnclick: (UIButton *) Sender { the * //removes the current $ [Self.curVC.view Removefromsuperview];Panax Notoginseng - //Remove the angle Mark theNsinteger index =[Sender.superview.subviews Indexofobject:sender]; + //remove the corresponding VC from the collection according to the angle label ASELF.CURVC =Self.childviewcontrollers[index]; the +Self.curVC.view.frame =self.contentView.bounds; - [Self.contentview AddSubview:self.curVC.view]; $ $ -Catransition *anim =[catransition animation]; -Anim.type =@"Cube"; theAnim.subtype = index > Self.oldindex?Kcatransitionfromright:kcatransitionfromleft; -Anim.duration =0.5;Wuyi [Self.contentView.layer Addanimation:anim forkey:nil]; theSelf.oldindex =index; - Wu } - About @end
View Code
Second, summary
- If the two controlled view is a parent-child relationship (either directly or indirectly), then the two controllers should also be parent-child relationships
[A.view Addsubview:b.view]; [a addchildviewcontroller:b]; // or [A.view Addsubview:otherview]; [Otherview Addsubbiew.b.view]; [a addchildviewcontroller:b];
- Get all the sub-controllers
@property (nonatomic,readonly) Nsarray *childviewcontrollers;
- Add a sub-controller
// Xmgoneviewcontroller becomes the child controller of self // Self becomes the parent controller of the Xmgoneviewcontroller [self Addchildviewcontroller:[[xmgoneviewcontroller alloc] init]]; // controllers added through Addchildviewcontroller will be present in the childviewcontrollers array
- Get Parent Controller
@property (nonatomic,readonly) Uiviewcontroller *parentviewcontroller;
- Remove a controller from its parent controller
// controller A is removed from its parent controller [A removefromparentviewcontroller];
iOS Edge Learning-the switch of a parent-child controller's custom controller