ios - 在 AppDelegate 中创建全局委托(delegate)类

标签 ios objective-c appdelegate

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

AppDelegate.h

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


…other init stuff

AppDelegate.m

  • 包括 SomeFrameworkDelegateOtherFrameworkDelegate 方法。
  • 这些方法主要输出连接/断开服务等消息。

主视图 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)函数。实现后您有多种选择。

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

关于ios - 在 AppDelegate 中创建全局委托(delegate)类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42537025/

相关文章:

ios - 自动布局有问题——简单的计算器应用程序

ios - 添加解析框架防止 pch 文件预编译

macos - 为什么在我的基于 macOS 文档的应用程序中没有通过 Storyboard 调用 applicationDidFinishLaunching?

iphone - 在 iOS 中保存加密 key 的安全方法

ios - 要在图像集中上传哪 3 个尺寸/分辨率图像以支持所有设备?

ios - ld : library not found for -lCocoaLumberjack

iphone - 如何旋转 UIWebView 嵌入的视频

ios - 从 AppDelegate 自定义每个 UISearchBar 不工作

ios - 导航 Controller 的最佳实践

objective-c - 覆盖文件中的当前内容