最好在NSNotificationCenter中调用选择器方法ViewWillAppear

时间:2017-01-03 12:07:55

标签: objective-c nsnotificationcenter

我想知道在NSNotificationCenter defaultCenter中调用viewWillAppear:方法是否合适。

[[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是一种不好的实施方式?

1 个答案:

答案 0 :(得分:4)

  1. viewWillAppeartemplate method,操作系统会为您调用它,您不应该自己手动调用它。

  2. 在视图消失之前,在viewWillAppear lifecycle中调用UIViewController被调用两次会破坏层次结构,这可能会导致一些非常奇怪的行为。

  3. 调试自己的UIViewController子类或任何子类将是一场噩梦。

  4. 根据您的建议,使用setUpInitialization()功能执行第二个选项,并在收到UIApplicationDidBecomeActiveNotification时执行所有操作。