在 AppDelegate 中创建全局委托 class
Creating a global delegate class inside AppDelegate
如果问题标题准确,请提前致歉。我在下面概述了我想要实现的基本流程。
AppDelegate.h
@interface AppDelegate : UIResponder <UIApplicationDelegate, SomeFrameworkDelegate, OtherFrameworkDelegate>
…other init stuff
AppDelegate.m
- 包括
SomeFrameworkDelegate
和 OtherFrameworkDelegate
方法。
- 这些方法主要输出连接/断开服务等消息。
MainViewController
- 使用这些委托方法做事。
- 连接/断开时执行 UI 操作。等等
DetailsViewController
- 使用与 MainViewController 中相同的内容。
备注:
我假设我将在 AppDelegate 中设置全局变量,然后在其他 UI 代码中,我将创建一个 AppDelegate 实例:
AppDelegate *appDelegate=(AppDelegate *)[[UIApplication sharedApplication]delegate];
appDelegate.isConnected = ...
appDelegate.serviceName = ...
我如何检测消息何时到达委托方法? NSNotificationCenter/ NSTimer?
即post 触发委托方法后的通知或创建计时器以轮询 appDelegate 中的变量。
你真的回答了你自己的问题。当它被触发时,我会在委托方法中使用 NSNotificationCenter
到 post 通知。然后,在您的视图控制器中,观察该通知并做出响应。
听起来您正试图 handle/manage 在您的应用程序委托中建立连接。 AppDelegate 的目的是响应 applicationdidbecomeactive/applicationwillenterforeground 等应用程序级别的事件。我的建议是您创建一个单例来管理您的连接。这通常称为 sharedInstance 模式。这个单例应该实现 managedObject 的委托函数。实施后您有多种选择。
- 键值观察或 KVO (http://nshipster.com/key-value-observing/)
- NSNotificationCenter (http://nshipster.com/nsnotification-and-nsnotificationcenter/)
在这种情况下,我建议使用 NSNotification,因为您正试图找出连接状态的变化。如果您希望在对象属性的每次更改时都收到通知,那么您应该使用 KVO。
如果问题标题准确,请提前致歉。我在下面概述了我想要实现的基本流程。
AppDelegate.h
@interface AppDelegate : UIResponder <UIApplicationDelegate, SomeFrameworkDelegate, OtherFrameworkDelegate>
…other init stuff
AppDelegate.m
- 包括
SomeFrameworkDelegate
和OtherFrameworkDelegate
方法。 - 这些方法主要输出连接/断开服务等消息。
MainViewController
- 使用这些委托方法做事。
- 连接/断开时执行 UI 操作。等等
DetailsViewController
- 使用与 MainViewController 中相同的内容。
备注:
我假设我将在 AppDelegate 中设置全局变量,然后在其他 UI 代码中,我将创建一个 AppDelegate 实例:
AppDelegate *appDelegate=(AppDelegate *)[[UIApplication sharedApplication]delegate]; appDelegate.isConnected = ... appDelegate.serviceName = ...
我如何检测消息何时到达委托方法? NSNotificationCenter/ NSTimer?
即post 触发委托方法后的通知或创建计时器以轮询 appDelegate 中的变量。
你真的回答了你自己的问题。当它被触发时,我会在委托方法中使用 NSNotificationCenter
到 post 通知。然后,在您的视图控制器中,观察该通知并做出响应。
听起来您正试图 handle/manage 在您的应用程序委托中建立连接。 AppDelegate 的目的是响应 applicationdidbecomeactive/applicationwillenterforeground 等应用程序级别的事件。我的建议是您创建一个单例来管理您的连接。这通常称为 sharedInstance 模式。这个单例应该实现 managedObject 的委托函数。实施后您有多种选择。
- 键值观察或 KVO (http://nshipster.com/key-value-observing/)
- NSNotificationCenter (http://nshipster.com/nsnotification-and-nsnotificationcenter/)
在这种情况下,我建议使用 NSNotification,因为您正试图找出连接状态的变化。如果您希望在对象属性的每次更改时都收到通知,那么您应该使用 KVO。