IOS study notes -- use ChildViewController and iosviewcontroller

Source: Internet
Author: User

IOS study notes -- use ChildViewController and iosviewcontroller

You have encountered problems when using TableView before. You need to use another TableViewController to store TableView first. The original View uses ViewContainer to reference TableViewController. In this case, the first access to one ViewController is to use another ViewController. Later, ChildViewController was also needed for other problems during development. In this case, you can use custom views to solve these problems. During Android development, a layout file can be specified for the custom View, but iOS cannot specify a layout file for the custom View. It is very difficult to implement the control layout by code, search for problems related to ViewContainer.

ViewContainer is used to add a sub-ViewController to ViewController. ViewContainer can be used in the visual StoryBoard, but it is more convenient to use ChildViewController if you use pure code control.

ChildViewController is a new feature of iOS5. iOS5 adds five methods and an attribute to UIViewController.

// Method addChildViewController: removeFromParentViewController: transitionFromViewController: toViewController: duration: options: animations: completion: attributes: @ property (nonatomic, readonly) NSArray * childViewControllers

In my opinion, the above method attributes can be described as simple as they are. The methods are used to add ChildViewController, remove ChildViewController, and switch ChildViewController. The following two methods are event-specific, triggered when ChildViewController switches to the main ViewController and after switching.

However, my current application scenario is to put ChildView in ScrollView to achieve page flip.

Two viewcontrollers are added to the StoryBoard. One is UIScrollView added to the master, and the other is ChildViewController added to ParentViewController.

First, add the name to the StroyBoard of ChildView. Then, you can construct the ViewController according to the StoryBoardID when constructing the ViewController in the main ViewController.

Because UIScrollView implements the page flip function, you need to configure it as follows:

self.scrollView.contentSize=CGSizeMake(self.view.frame.size.width*pagecount, self.scrollView.frame.size.height);self.scrollView.pagingEnabled=true;

The code for adding ChildViewController is as follows:

SunRealAQIViewController *realCV2=[[self storyboard]instantiateViewControllerWithIdentifier:@"test123"];realCV2.view.frame=CGRectMake(self.view.frame.size.width, 0, self.view.frame.size.width, self.view.frame.size.height);[self addChildViewController:realCV2];[self.scrollView addSubview:realCV2.view];

First, construct the ChildViewController and set its frame attribute. In this case, the first page may not be set, but the third page on the second page needs to be set, the third line is to call the addChildViewController method of UIViewController to add the ChildViewController. The last line is to add the ChildView View to the specified position of the main view. Here we want to add it to the ScrollView, therefore, we call [self. scrollView addSubver:] method. Multiple identical childviewcontrollers must be added to the ScrollView, and a loop must be used.

    for (int i=0; i<pagecount; i++){    SunRealAQIViewController *realCV2=[[self storyboard]instantiateViewControllerWithIdentifier:@"test123"];    realCV2.view.frame=CGRectMake(self.view.frame.size.width*i, 0, self.view.frame.size.width, self.view.frame.size.height);    [self addChildViewController:realCV2];    [self.scrollView addSubview:realCV2.view];} 

In this way, the door to ChildViewController is opened!

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.