objective-c - 委托(delegate)单例对象

标签 objective-c ios cocoa-touch

我有一个 NSObject,它是一个单例。这个单例类有一个委托(delegate)有什么问题吗?我担心单例类型会失败。

这是我的场景。我有一个函数(在这个单例类中)执行异步请求以从 API 中提取 NSDictionary。基本上当这个请求完成时,我想通知一个类请求已经完成。

最佳答案

不,委托(delegate)不会失败,但考虑使用 NSNotificationCenter相反:

static NSString *const kMyClassNotificationName = @"myClassNotificationName";

// where you would call a delegate method (e.g. [self.delegate doSomething])
[[NSNotificationCenter defaultCenter] postNotificationName:kMyClassNotificationName object:self userInfo: /* dictionary containing variables to pass to the delegate */];

// where you would set up a delegate (e.g. [Singleton instance].delegate = self)
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(doSomething) name:kMyClassNotificationName object:[Singleton instance]];

关于objective-c - 委托(delegate)单例对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9475469/

相关文章:

iphone - 如何在 iPhone 上改变 View 时制作溶解动画?

iphone - 不可删除的 iOS 应用程序可能吗?

ios - 如何在 UIscrollView 中获取当前控制位置?

ios - UILocalNotification 在后台 10 分钟后不提示

ios - UIFileSharingEnabled 在我的 ios cordova 应用程序中没有效果

iphone - 如何在 uisearchbar ios 中添加 DropDownComobo

ios - 如何从我的 iPad 应用程序 (Objective-C) 打开设置

iphone - 在使用 ARC 时将属性分配给自动释放的初始化程序的结果是否安全?

ios - 系统跟踪工具中的时间线是什么意思

ios - NSInvalidArgumentException',原因 : '-[__NSArrayI length : unrecognized selector sent to instance