IOS addChildViewController method

APP often have to switch pages according to the label requirements, if you switch the page just to refresh the data is just, but if the data and content, each label switching structure of the page is totally different from what you have done? (for example: Figure 1-1)

IOS addChildViewController method
Figure 1-1

Personally feel that the ideal approach is to display the contents of each label as a View, before this switch does not affect View can fast cut back before View, and in accordance with the development of high and low coupling polymerization, ah, here to introduce addChildViewController method:

/ / add other UIViewController in ViewController, currentVC is a UIViewController variable to store the current display viewcontroller FirstVC * first = [[FirstVC alloc] init]; [self addChildViewController:first]; //addChildViewController willMoveToParentViewController:self] calls the [child method, but does not call the didMoveToParentViewController: method, the official advice show called [first didMoveToParentViewController:self]; [first.view setFrame:CGRectMake (0, CGRectGetMaxY, width, height-CGRectGetHeight (myScrollView.frame) (myScrollView.frame))]; currentVC = first; [self.view addSubview:currentVC.view] addSubview:; / / there is no other way, only one, and can switch views, isn't it amazing? Second = [[SecondVC init]; [second.view setFrame:CGRectMake (0, CGRectGetMaxY (myScrollView.frame), width, height-CGRectGetHeight (myScrollView.frame)) (alloc]);

Apple has given me a good way to switch UIViewController transitionFromViewController:

#pragma mark - viewController - switching (void) changeControllerFromOldController: (UIViewController * oldController) toNewController: (UIViewController * newController) {[self addChildViewController:newController]; / * * * * transitionFromViewController:oldController toViewController:newController switch ViewController [self duration:0.3 options:UIViewAnimationOptionCurveEaseIn animations:^{/ do some animation (BOOL finished) {completion:^} if (finished) {/ / remove oldController, but in removeFromParentViewController, call the willMoveToParentViewController:nil method methods not, so need to show called [self didMoveToParentViewController:newController]; [oldCont Roller willMoveToParentViewController:nil]; [oldController removeFromParentViewController]; currentVC = newController;}else {currentVC = oldController;}}}];

The results are as follows:

IOS addChildViewController method
Figure 1-2 featured page
IOS addChildViewController method
diagram 1-3 switch to discovery page

Here we have some knowledge about addChildViewController, when an interface is relatively complex and we can use this way to reduce the degree of coupling (methods, if you have more good hope don’t begrudge exchange) to do so, the page logic more clear, if there can be reuse is also convenient to reuse. But View did not show nor load, reducing memory usage.
at the same time, you can also add a number of child parent on a ViewController, in fact, such a page is quite a lot, as shown in Figure 1-4

/ / add other UIViewController * first = FirstVC [[FirstVC alloc] init] in ViewController; [self addChildViewController:first]; //addChildViewController willMoveToParentViewController:self] calls the [child method, but does not call the didMoveToParentViewController: method, the official advice show called [self didMoveToParentViewController:first]; [first.view setFrame:CGRectMake (0, CGRectGetMaxY (myScrollView.frame), width, 300)]; [self.view addSubview:first.view]; SecondVC * second = [[SecondVC alloc] init] [self; addChildViewController:second]; [self didMoveToParentViewController:second]; [second.view setFrame:CGRectMake (0, CGRectGetMaxY (first.view.frame), width, 300)]; [self.view addSubview:second. View];
IOS addChildViewController method
figure 1-4