Application of Childviewcontroller
Viewcontrolle can add more than one subview to display when needed, the other way is to control the Subview in the page by adding multiple childciewcontroller to Parentviewcontroller; reduce code coupling; by toggling the sub-view controller, you can display different views, replacing the management of previous addsubview.
This section demonstrates the application of Chileviewcontroller through a interface similar to the Baidu News module switch:
Document structure:
Code Demo:
#import "MainViewController.h" #import "FirstViewController.h" #import "SecondViewController.h" #import " ThirdViewController.h "@interface Mainviewcontroller () @property (nonatomic, strong) Firstviewcontroller *firstvc;@ Property (Nonatomic, Strong) Secondviewcontroller *SECONDVC; @property (nonatomic, strong) Thirdviewcontroller *thirdVC ; @property (nonatomic, strong) Uiviewcontroller *CURRENTVC; @property (nonatomic, strong) Uiscrollview *headscrollview; @property (nonatomic, strong) Nsmutablearray *itemarray;@ Property (Nonatomic, Strong) UIView *contentview; @end @implementation mainviewcontroller-(void) loadview{[Super Loadvi EW]; [Self initialization];} -(void) viewdidload {[Super viewdidload]; [Self loadbaseui];} -(void) initialization{_itemarray = [Nsmutablearray arraywithobjects:@ "headline" @ "Today", @ "Focus", nil];}-(void) loadbaseui{ Self.title = @ "Home"; _headscrollview = [[Uiscrollview alloc]initwithframe:cgrectmake (0, 0, [UIScreen mainscreen].bounds.size.width, 44)]; _hEadscrollview.backgroundcolor = [Uicolor colorwithwhite:0.902 alpha:1.000]; for (int i = 0; i<_itemarray.count; i++) {UIButton *itembutton = [[UIButton Alloc]initwithframe:cgrectmake (i* ([ UIScreen Mainscreen].bounds.size.width/_itemarray.count), 0, [UIScreen mainscreen].bounds.size.width/_ Itemarray.count, 44)]; Itembutton.tag = 100+i; Itembutton.backgroundcolor = [Uicolor Clearcolor]; Nsdictionary *dic = @{nsforegroundcolorattributename:[uicolor Purplecolor],nsfontattributename:[uifont SYSTEMFONTOFSIZE:14.0F]}; [Itembutton setattributedtitle:[[nsattributedstring alloc]initwithstring:_itemarray[i] attributes:dic] ForState: UIControlStateNormal]; [Itembutton addtarget:self Action: @selector (ButtonClick:) forcontrolevents:uicontroleventtouchupinside]; [_headscrollview Addsubview:itembutton]; } [_headscrollview Setcontentsize:cgsizemake ([UIScreen mainscreen].bounds.size.width, 44)]; _headscrollview.showshorizontalscrollindicator = NO; _headscrollview.showsverticalscrollindicator = NO; [Self.view Addsubview:_headscrollview]; _contentview = [[UIView alloc]initwithframe:cgrectmake (0, UIScreen mainscreen].bounds.size.width, [UIScreen MAINSCREEN].BOUNDS.SIZE.HEIGHT-44-64)]; _contentview.backgroundcolor = [Uicolor Clearcolor]; [Self.view Addsubview:_contentview]; [Self addsubcontrollers];} #pragma mark-privatemethods-(void) addsubcontrollers{_FIRSTVC = [[Firstviewcontroller alloc]initwithnibname:@ ' first Viewcontroller "Bundle:nil"; [Self ADDCHILDVIEWCONTROLLER:_FIRSTVC]; _SECONDVC = [[Secondviewcontroller alloc]initwithnibname:@ "Secondviewcontroller" bundle:nil]; [Self ADDCHILDVIEWCONTROLLER:_SECONDVC]; _THIRDVC = [[Thirdviewcontroller alloc]initwithnibname:@ "Thirdviewcontroller" bundle:nil]; [Self ADDCHILDVIEWCONTROLLER:_THIRDVC]; Adjust the frame of the child view controller to fit the container view [self FITFRAMEFORCHILDVIEWCONTROLLER:_FIRSTVC]; Set the content displayed by default in the container view [Self.contentview addSubview:_firstvc.view]; NSLog (@ "%@", Nsstringfromcgrect (Self.contentView.frame)); NSLog (@ "%@", Nsstringfromcgrect (_firstvc.view.frame)); _CURRENTVC = _FIRSTVC;} -(void) ButtonClick: (UIButton *) sender{if (Sender.tag = = && _CURRENTVC = _FIRSTVC) | | (Sender.tag = = 101 && _CURRENTVC = = _SECONDVC) | | (Sender.tag = = 102 && _CURRENTVC = = _THIRDVC)) {return; } switch (Sender.tag) {case 100:{[self FITFRAMEFORCHILDVIEWCONTROLLER:_FIRSTVC]; [Self TRANSITIONFROMOLDVIEWCONTROLLER:_CURRENTVC TONEWVIEWCONTROLLER:_FIRSTVC]; } break; Case 101:{[self FITFRAMEFORCHILDVIEWCONTROLLER:_SECONDVC]; [Self TRANSITIONFROMOLDVIEWCONTROLLER:_CURRENTVC TONEWVIEWCONTROLLER:_SECONDVC]; } break; Case 102:{[self FITFRAMEFORCHILDVIEWCONTROLLER:_THIRDVC]; [Self TRANSITIONFROMOLDVIEWCONTROLLER:_CURRENTVC TONEWVIEWCONTROLLER:_THIRDVC]; } break; }}-(void) Fitframeforchildviewcontroller: (Uiviewcontroller *) chileviewcontroller{cgrect frame = Self.contentView.frame; FRAME.ORIGIN.Y = 0; ChileViewController.view.frame = frame;} Convert Child View Controller-(void) Transitionfromoldviewcontroller: (Uiviewcontroller *) Oldviewcontrollertonewviewcontroller: ( Uiviewcontroller *) newviewcontroller{[self Transitionfromviewcontroller:oldviewcontroller toViewController: Newviewcontroller duration:0.3 options:uiviewanimationoptiontransitioncrossdissolve Animations:nil completion:^ ( BOOL finished) {if (finished) {[Newviewcontroller didmovetoparentviewcontroller:self]; _CURRENTVC = Newviewcontroller; }else{_CURRENTVC = Oldviewcontroller; } }];} Remove all child view controllers-(void) removeallchildviewcontrollers{for (Uiviewcontroller *VC in self.childviewcontrollers) {[VC Willmovetoparentviewcontroller:nil]; [VC Removefromparentviewcontroller]; }}/** * Method Description: * 1, Addchildviewcontroller: Add a child VC to the parent VC, and then automatically call Willmovetoparentviewcontroller after adding: Parent VC * 2, Removefromparentviewcontroller: Remove the child VC from the parent VC and automatically call Didmovetoparentviewcontroller:nil * 3 after removal Willmovetoparentviewcontroller: This method is called automatically when a child VC is added to the parent VC. To remove a child VC from a parent VC, you need to call the method before removing it, passing in the parameter nil. * 4, Didmovetoparentviewcontroller: After adding a child VC to the parent VC, the method will not be automatically called, need to show the call to tell the compiler has finished adding (in fact, do not call the method is not a problem, not very clear); After the child VC is removed from the parent VC, the method is automatically called and the parameter passed in is nil, so there is no need to display the call. * */** * NOTE: To switch child View controller a/b,a/b must have been added to the parent view controller, otherwise it will be an error */@end
Final effect: ( enables switching between 3 views )
iOS Development-Switch to view with Childviewcontroller