2017-01-03 24 views
0

我想知道在NSNotificationCenter defaultCenter中調用viewWillAppear:方法是否好。很好的調用選擇器方法ViewWillAppear在NSNotificationCenter

[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(viewWillAppear:) name:UIApplicationDidBecomeActiveNotification object:nil]; 

或者

-(void)setUpInitialization{ 
// dump code here in ViewWillAppears. 
} 

調用方法setUpInitialization

[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(setUpInitialization) name:UIApplicationDidBecomeActiveNotification object:nil]; 

如果直接調用viewWillAppear是實現一個沒有好辦法?

+0

當應用程序變爲活動狀態時,系統是否已經調用了「viewWillAppear:'? – mattsson

+0

@mattsson是的,應用程序變成活動的tabbarcontroller。 – kiran

+0

那麼爲什麼使用通知再次調用它? – mattsson

回答

4

NO

  1. viewWillAppeartemplate method,操作系統將調用它適合你,你永遠不應該手動你自己調用它。

  2. 視圖就會消失之前,調用viewWillAppear在兩次UIViewControllerlifecycle將打破層級,它可能會導致一些非常奇怪的行爲被調用。

  3. 調試你自己的UIViewController子類或任何子類將是一場噩夢。

正如你所提出的建議,做使用setUpInitialization()函數的第二個選項,並做一切在那裏,當您收到UIApplicationDidBecomeActiveNotification

+0

感謝您的信息。 – kiran

相關問題