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

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

1.新建一個項目

2.在storyboard里拖一個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個子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.查看調(diào)用棧


Screen Shot 2016-12-01 at 15.14.33.png

9.我們發(fā)現(xiàn)這個在第六個是setSelectedViewController, 接著調(diào)用了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.在自己代碼中實現(xiàn), 2個childViewController切換的時候,只要調(diào)用這個方法, 確實會調(diào)用viewWillAppear,viewWillDisappear,viewDidAppearviewDidDisappear

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末救斑,一起剝皮案震驚了整個濱河市歹啼,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌锉矢,老刑警劉巖,帶你破解...
    沈念sama閱讀 217,734評論 6 505
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件夫晌,死亡現(xiàn)場離奇詭異授艰,居然都是意外死亡,警方通過查閱死者的電腦和手機症歇,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,931評論 3 394
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來谭梗,“玉大人忘晤,你說我怎么就攤上這事〖つ螅” “怎么了设塔?”我有些...
    開封第一講書人閱讀 164,133評論 0 354
  • 文/不壞的土叔 我叫張陵,是天一觀的道長远舅。 經(jīng)常有香客問我闰蛔,道長,這世上最難降的妖魔是什么图柏? 我笑而不...
    開封第一講書人閱讀 58,532評論 1 293
  • 正文 為了忘掉前任钞护,我火速辦了婚禮,結(jié)果婚禮上爆办,老公的妹妹穿的比我還像新娘难咕。我一直安慰自己,他們只是感情好距辆,可當我...
    茶點故事閱讀 67,585評論 6 392
  • 文/花漫 我一把揭開白布余佃。 她就那樣靜靜地躺著,像睡著了一般跨算。 火紅的嫁衣襯著肌膚如雪爆土。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,462評論 1 302
  • 那天诸蚕,我揣著相機與錄音步势,去河邊找鬼氧猬。 笑死,一個胖子當著我的面吹牛坏瘩,可吹牛的內(nèi)容都是我干的盅抚。 我是一名探鬼主播,決...
    沈念sama閱讀 40,262評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼倔矾,長吁一口氣:“原來是場噩夢啊……” “哼妄均!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起哪自,我...
    開封第一講書人閱讀 39,153評論 0 276
  • 序言:老撾萬榮一對情侶失蹤丰包,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后壤巷,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體邑彪,經(jīng)...
    沈念sama閱讀 45,587評論 1 314
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,792評論 3 336
  • 正文 我和宋清朗相戀三年胧华,在試婚紗的時候發(fā)現(xiàn)自己被綠了寄症。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 39,919評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡撑柔,死狀恐怖瘸爽,靈堂內(nèi)的尸體忽然破棺而出您访,到底是詐尸還是另有隱情铅忿,我是刑警寧澤,帶...
    沈念sama閱讀 35,635評論 5 345
  • 正文 年R本政府宣布灵汪,位于F島的核電站檀训,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏享言。R本人自食惡果不足惜峻凫,卻給世界環(huán)境...
    茶點故事閱讀 41,237評論 3 329
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望览露。 院中可真熱鬧荧琼,春花似錦、人聲如沸差牛。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,855評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽偏化。三九已至脐恩,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間侦讨,已是汗流浹背驶冒。 一陣腳步聲響...
    開封第一講書人閱讀 32,983評論 1 269
  • 我被黑心中介騙來泰國打工苟翻, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人骗污。 一個月前我還...
    沈念sama閱讀 48,048評論 3 370
  • 正文 我出身青樓崇猫,卻偏偏與公主長得像,于是被迫代替她去往敵國和親身堡。 傳聞我的和親對象是個殘疾皇子邓尤,可洞房花燭夜當晚...
    茶點故事閱讀 44,864評論 2 354

推薦閱讀更多精彩內(nèi)容