關于UITabbarController為什么每次調用viewWillAppear

先說結論: 是因為UITabbarController在切換viewController時, 調用了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的時候, 會調用viewWillAppear,viewWillDisappear,viewDidAppearviewDidDisappear. 有時候, 我們的需求是在navigationBar的title位置加一個UISegmentedControl, 來實現切換viewController, 并且還要有UIScrollView的效果. 我們可以在一個大的viewControlleraddChildViewController來實現.但是卻不會調用viewWillAppear之類的方法. 那么UITabbarController是如何實現的呢?
讓我們一步步來探究

1.新建一個項目

2.在storyboard里拖一個UITabbarController, 并設為entry point

Screen Shot 2016-12-01 at 15.05.41.png

3.創建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是一樣的, 只是數字變成1

6.把storyboard中的2個子viewController綁定代碼

Screen Shot 2016-12-01 at 15.09.56.png

7.在ViewController2中的viewWillAppear方法打斷點.

Screen Shot 2016-12-01 at 15.11.04.png

8.查看調用棧


Screen Shot 2016-12-01 at 15.14.33.png

9.我們發現這個在第六個是setSelectedViewController, 接著調用了transitionFromViewController:toViewController, 這是什么方法, 沒聽說過?

10.UITabBarController *v = nil; [v transitionFromViewController:nil toViewController:nil duration:0 options:0 animations:nil completion:nil]; 手動試一下這個方法看看是否公開

11.command點擊進入這個方法

/*
  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.在自己代碼中實現, 2個childViewController切換的時候,只要調用這個方法, 確實會調用viewWillAppear,viewWillDisappear,viewDidAppearviewDidDisappear

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容