iOS development small function of 9:5 code to take care of simple parent-child controller

Source: Internet
Author: User

Small size elder brother Big God code, indeed streamlined!

1, the final results such as the following three diagram, click One,two,three, respectively, 3 different controllers appear

Direct code: (three controller created on it)

#import "ViewController.h"#import "ZWOneViewController.h"#import "ZWTwoViewController.h"#import "ZWThreeViewController.h"@interfaceViewcontroller ()/** The controller being displayed*/@property (Weak, nonatomic) Uiviewcontroller*SHOWINGVC;/** Controller Array*/@property (Strong, nonatomic) Nsarray*allvcs;//three properties in place of a controller@end@implementationViewcontroller- (void) viewdidload {[Super viewdidload]; Self.allvcs=@[[[Zwoneviewcontroller alloc] init], [[Zwtwoviewcontroller alloc] init], [[Zwthreeviewcontroller alloc] init];}-(Ibaction) ButtonClick: (UIButton *) button {//removing the currently displayed controller[Self.showingVC.view Removefromsuperview]; //get the position index of the controllerNsuinteger index =[Button.superview.subviews Indexofobject:button]; //Add Controller ViewSELF.SHOWINGVC =Self.allvcs[index]; //Set DimensionsSelf.showingVC.view.frame = CGRectMake (0, -, Self.view.frame.size.width, Self.view.frame.size.height- -); //adding to the controller[Self.view AddSubview:self.showingVC.view];}

Note: 1, extensibility is very good, directly in the array to add the controller need to add

2, because is the index, must pay attention to the order of three controllers, otherwise there will be a click after the other controller. Such as:

iOS development small function of 9:5 code to take care of simple parent-child 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: 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.