溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶(hù)服務(wù)條款》

iphone開(kāi)發(fā)之解決viewWillAppear失效

發(fā)布時(shí)間:2020-03-27 08:44:13 來(lái)源:網(wǎng)絡(luò) 閱讀:3332 作者:arthurchen 欄目:開(kāi)發(fā)技術(shù)

你可曾遇到過(guò)viewWillAppear沒(méi)有被調(diào)用到的情況

產(chǎn)生原因是用了UINavigationController. 
將UINavigationController的view作為subview添加到了其他viewController的view中。
或者把UINavigationController添加到UITabbarController中了。

此時(shí),NavigationController的stack里面的viewController就收不到-(void)viewWillAppear:(BOOL)animated;等4個(gè)方法的調(diào)用。

 

原因呢

 

Apple Docs state:

 

Warning: If the view belonging to a view controller is added to a view hierarchy directly, the view controller will not receive this message. If you insert or add a view to the view hierarchy, and it has a view controller, you should send the associated view controller this message directly. Failing to send the view controller this message will prevent any associated animation from being displayed.

不過(guò)后面的到4.0的文檔就沒(méi)有發(fā)現(xiàn)這樣的文字描述了,但是還是沒(méi)能夠調(diào)用的到這樣

只是添加的更復(fù)雜的文檔,頭暈暈看不下去了。

 

解決方法兩種:
1,在導(dǎo)航控制器上層controller的viewWillAppear中顯式調(diào)用viewWillAppear方法。

-(void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated]; [subNavCntlr viewWillAppear:animated];
}

- (void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
[subNavCntlr viewWillDisappear:animated];
}

2,把導(dǎo)航控制器上層controller設(shè)為UINavigationController的delegate

nav.delegate = self;

@interface RootViewController : UIViewController <UINavigationControllerDelegate> { UINavigationController *navController; }

 

- (void )navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL )animated { [viewController viewWillAppear:animated]; } - (void )navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL )animated { [viewController viewDidAppear:animated]; }

湊合著用吧,我喜歡第二種,不過(guò)我更喜歡干脆不再viewWillappear里做事情了。

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI