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!