有没有办法知道观察者是否收到NSNotificationCenter的通知?

时间:2017-01-15 14:01:56

标签: ios objective-c nsnotificationcenter nsnotification

ViewController.m[NSNotificationCenter defaultCenter]注册为观察员并且正在运作。但是ViewControllerviewDidLoad方法中执行了X.

如果ViewController 没有收到通知,我希望X只发生

架构可能听起来确实破了,但我的问题是defaultCenter内是否有一些数据告诉我们观察员是否收到了通知?

2 个答案:

答案 0 :(得分:0)

我们没有回叫方法可以知道观察者是否收到了被发送的notification

根据我们可以在Xcode中获得的Notification Center方法:

/****************   Notification Center ****************/
open class NotificationCenter : NSObject {


    open class var `default`: NotificationCenter { get }


    open func addObserver(_ observer: Any, selector aSelector: Selector, name aName: NSNotification.Name?, object anObject: Any?)


    open func post(_ notification: Notification)

    open func post(name aName: NSNotification.Name, object anObject: Any?)

    open func post(name aName: NSNotification.Name, object anObject: Any?, userInfo aUserInfo: [AnyHashable : Any]? = nil)


    open func removeObserver(_ observer: Any)

    open func removeObserver(_ observer: Any, name aName: NSNotification.Name?, object anObject: Any?)


    @available(iOS 4.0, *)
    open func addObserver(forName name: NSNotification.Name?, object obj: Any?, queue: OperationQueue?, using block: @escaping (Notification) -> Swift.Void) -> NSObjectProtocol
}

我们可以参考NotificationCenter' s apple docs,我们应该了解它的功能是:

  

对象向通知中心注册,以使用addObserver(_:selector:name:object :)或addObserver(forName:object:queue:using :)方法接收通知(NSNotification对象)。

从文档中找不到你的要求。

答案 1 :(得分:0)

如果您正在寻找回电,请考虑结合使用,它更像是一种事务性通信协议。

相关问题