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