先說(shuō)結(jié)論: 是因?yàn)閁ITabbarController在切換viewController時(shí), 調(diào)用了transitionFromViewController方法
/*
This method can be used to transition between sibling child view controllers. The receiver of this method is
their common parent view controller. (Use [UIViewController addChildViewController:] to create the
parent/child relationship.) This method will add the toViewController's view to the superview of the
fromViewController's view and the fromViewController's view will be removed from its superview after the
transition completes. It is important to allow this method to add and remove the views. The arguments to
this method are the same as those defined by UIView's block animation API. This method will fail with an
NSInvalidArgumentException if the parent view controllers are not the same as the receiver, or if the
receiver explicitly forwards its appearance and rotation callbacks to its children. Finally, the receiver
should not be a subclass of an iOS container view controller. Note also that it is possible to use the
UIView APIs directly. If they are used it is important to ensure that the toViewController's view is added
to the visible view hierarchy while the fromViewController's view is removed.
*/
- (void)transitionFromViewController:(UIViewController *)fromViewController toViewController:(UIViewController *)toViewController duration:(NSTimeInterval)duration options:(UIViewAnimationOptions)options animations:(void (^ __nullable)(void))animations completion:(void (^ __nullable)(BOOL finished))completion NS_AVAILABLE_IOS(5_0);
UITabbarController每次切換viewController的時(shí)候, 會(huì)調(diào)用viewWillAppear,viewWillDisappear,viewDidAppear和viewDidDisappear. 有時(shí)候, 我們的需求是在navigationBar的title位置加一個(gè)UISegmentedControl, 來(lái)實(shí)現(xiàn)切換viewController, 并且還要有UIScrollView的效果. 我們可以在一個(gè)大的viewController里addChildViewController來(lái)實(shí)現(xiàn).但是卻不會(huì)調(diào)用viewWillAppear之類的方法. 那么UITabbarController是如何實(shí)現(xiàn)的呢?
讓我們一步步來(lái)探究
1.新建一個(gè)項(xiàng)目
2.在storyboard里拖一個(gè)UITabbarController, 并設(shè)為entry point

3.創(chuàng)建ViewController1和ViewController2
4.ViewController1的代碼
- (void)viewDidLoad {
[super viewDidLoad];
NSLog(@"1viewDidLoad");
}
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
NSLog(@"1viewWillAppear");
}
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
NSLog(@"1viewDidAppear");
}
- (void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
NSLog(@"1viewWillDisappear");
}
- (void)viewDidDisappear:(BOOL)animated
{
[super viewDidDisappear:animated];
NSLog(@"1viewDidDisappear");
}
5.ViewController2的代碼和1是一樣的, 只是數(shù)字變成1
6.把storyboard中的2個(gè)子viewController綁定代碼

7.在
ViewController2中的viewWillAppear方法打斷點(diǎn).

8.查看調(diào)用棧

9.我們發(fā)現(xiàn)這個(gè)在第六個(gè)是setSelectedViewController, 接著調(diào)用了transitionFromViewController:toViewController, 這是什么方法, 沒(méi)聽(tīng)說(shuō)過(guò)?
10.UITabBarController *v = nil; [v transitionFromViewController:nil toViewController:nil duration:0 options:0 animations:nil completion:nil]; 手動(dòng)試一下這個(gè)方法看看是否公開(kāi)
11.command點(diǎn)擊進(jìn)入這個(gè)方法
/*
This method can be used to transition between sibling child view controllers. The receiver of this method is
their common parent view controller. (Use [UIViewController addChildViewController:] to create the
parent/child relationship.) This method will add the toViewController's view to the superview of the
fromViewController's view and the fromViewController's view will be removed from its superview after the
transition completes. It is important to allow this method to add and remove the views. The arguments to
this method are the same as those defined by UIView's block animation API. This method will fail with an
NSInvalidArgumentException if the parent view controllers are not the same as the receiver, or if the
receiver explicitly forwards its appearance and rotation callbacks to its children. Finally, the receiver
should not be a subclass of an iOS container view controller. Note also that it is possible to use the
UIView APIs directly. If they are used it is important to ensure that the toViewController's view is added
to the visible view hierarchy while the fromViewController's view is removed.
*/
- (void)transitionFromViewController:(UIViewController *)fromViewController toViewController:(UIViewController *)toViewController duration:(NSTimeInterval)duration options:(UIViewAnimationOptions)options animations:(void (^ __nullable)(void))animations completion:(void (^ __nullable)(BOOL finished))completion NS_AVAILABLE_IOS(5_0);
12.在自己代碼中實(shí)現(xiàn), 2個(gè)childViewController切換的時(shí)候,只要調(diào)用這個(gè)方法, 確實(shí)會(huì)調(diào)用viewWillAppear,viewWillDisappear,viewDidAppear和viewDidDisappear