在AppDelegate

时间:2017-03-01 16:30:11

标签: ios objective-c appdelegate

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

AppDelegate.h

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


…other init stuff

AppDelegate.m

  • 包含SomeFrameworkDelegateOtherFrameworkDelegate方法。
  • 这些方法主要输出消息,例如连接/断开服务。

MainViewController

  • 使用这些委托方法做事。
  • 连接/断开连接时执行UI操作。等

DetailsViewController

  • 使用与MainViewController中相同的内容。

备注:

  • 我假设我要在AppDelegate中设置全局变量,然后在其他UI代码中,我将创建AppDelegate的实例:

    AppDelegate *appDelegate=(AppDelegate *)[[UIApplication sharedApplication]delegate]; appDelegate.isConnected = ... appDelegate.serviceName = ...

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

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

2 个答案:

答案 0 :(得分:1)

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

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

答案 1 :(得分:0)

你真的回答了自己的问题。我会使用NSNotificationCenter在委托方法触发时发布通知。然后,在视图控制器中,观察该通知并进行响应。