關(guān)于UITabbarController為什么每次調(diào)用viewWillAppear

先說(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,viewDidAppearviewDidDisappear. 有時(shí)候, 我們的需求是在navigationBar的title位置加一個(gè)UISegmentedControl, 來(lái)實(shí)現(xiàn)切換viewController, 并且還要有UIScrollView的效果. 我們可以在一個(gè)大的viewControlleraddChildViewController來(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

Screen Shot 2016-12-01 at 15.05.41.png

3.創(chuàng)建ViewController1ViewController2

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綁定代碼

Screen Shot 2016-12-01 at 15.09.56.png

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

Screen Shot 2016-12-01 at 15.11.04.png

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


Screen Shot 2016-12-01 at 15.14.33.png

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,viewDidAppearviewDidDisappear

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容