iOS Edge Learning-the switch of a parent-child controller's custom controller

Source: Internet
Author: User

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

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.