【问题标题】:Calling methods of view controller from app delegate class从应用委托类调用视图控制器的方法
【发布时间】:2015-02-12 16:43:55
【问题描述】:

当我的应用程序进入前台时,我需要从应用程序委托类调用我的视图控制器的一些方法。最好的实现方式是什么?

我尝试在“applicationWillEnterForeground”方法中创建“假”通知:

[NSNotificationCenter defaultCenter]postNotificationName:@"test" object:nil];

并在视图控制器中添加观察者:

[self addObserver: self selector:@selector(testMethod) name:@"test" object:nil];

这样好吗?那里的另一种方法:Calling UIViewController method from app delegate

哪种方法更好,为什么?

PS 对不起,我的英语不好。

【问题讨论】:

标签: ios observers


【解决方案1】:

快速回答:

  1. 是的,这应该适用于您的“虚假”通知
  2. 但是为什么要这样做呢?你可以让你的 viewController 在没有 AppDelegate 的情况下做所有的事情 -> 只需调用你的方法,你想在你的 viewController 的“viewDidLoad”方法中完成。 (我假设,您的 viewController 是您的 App 的入口点)

【讨论】:

  • 加载时间并不是应用进入前台的唯一时间。
【解决方案2】:

Notification 将支持许多目的地,而答案 here 仅支持一个 viewController 被告知要做什么。

另一方面,通过引用直接调用更容易理解和调试。

我不明白你为什么称它为“假”通知 - 这正是该调用的用途。

【讨论】:

    猜你喜欢
    • 2011-06-26
    • 2015-08-22
    • 2011-12-29
    • 1970-01-01
    • 1970-01-01
    • 2012-01-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多