在 AppDelegate 中创建全局委托 class

Creating a global delegate class inside AppDelegate

如果问题标题准确,请提前致歉。我在下面概述了我想要实现的基本流程。

AppDelegate.h

@interface AppDelegate : UIResponder <UIApplicationDelegate, SomeFrameworkDelegate, OtherFrameworkDelegate>


…other init stuff

AppDelegate.m

MainViewController

DetailsViewController

备注:

我如何检测消息何时到达委托方法? NSNotificationCenter/ NSTimer?

即post 触发委托方法后的通知或创建计时器以轮询 appDelegate 中的变量。

你真的回答了你自己的问题。当它被触发时,我会在委托方法中使用 NSNotificationCenter 到 post 通知。然后,在您的视图控制器中,观察该通知并做出响应。

听起来您正试图 handle/manage 在您的应用程序委托中建立连接。 AppDelegate 的目的是响应 applicationdidbecomeactive/applicationwillenterforeground 等应用程序级别的事件。我的建议是您创建一个单例来管理您的连接。这通常称为 sharedInstance 模式。这个单例应该实现 managedObject 的委托函数。实施后您有多种选择。

在这种情况下,我建议使用 NSNotification,因为您正试图找出连接状态的变化。如果您希望在对象属性的每次更改时都收到通知,那么您应该使用 KVO。