【问题标题】:Is good to call selector method ViewWillAppear in NSNotificationCenter在 NSNotificationCenter 中调用选择器方法 ViewWillAppear 好
【发布时间】:2017-01-03 23:24:21
【问题描述】:

我想知道在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是不是很好的实现方式?

【问题讨论】:

  • 当应用激活时,viewWillAppear: 不是已经被系统调用了吗?
  • @mattsson 是的,应用程序已为 tabbarcontroller 激活。
  • 那为什么要使用通知再次调用它呢?
  • 我不建议直接调用viewWillAppear - 首先它会破坏视图生命周期层次结构调用,其次如果有人从你的类中子类他可能很难调试他自己的类,因为viewWillAppear 将比平时更频繁地打电话。

标签: objective-c nsnotificationcenter


【解决方案1】:

  1. viewWillAppeartemplate method,操作系统会为你调用它,你千万不要自己手动调用它。

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

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

按照您的建议,使用setUpInitialization() 函数执行第二个选项,并在收到UIApplicationDidBecomeActiveNotification 时执行所有操作。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多