Swift NSNotificationCenter不会触发

时间:2015-02-11 15:45:29

标签: ios swift

我在使用swift和NSNotificationCenter-System时遇到了一些问题。 我添加了Observer:

NSNotificationCenter.defaultCenter().addObserverForName("disconnected", object: nil, queue: nil) { note in
        self.btConnect.title = "Verbinden"
}

我发布了这样的通知:

NSNotificationCenter.defaultCenter().postNotificationName("disconnect", object: self)

但没有任何事情发生。 Observer和Notifier属于不同的类。 有人可以帮助我吗,我做错了什么?

2 个答案:

答案 0 :(得分:3)

您的通知名称不正确:"已断开连接" vs"断开"。

答案 1 :(得分:0)

您的两种方法都需要具有相同的名称。目前,他们是disconnecteddisconnect。您必须更改postNotificationName参数:

NSNotificationCenter.defaultCenter().postNotificationName("disconnect", object: self)

要:

NSNotificationCenter.defaultCenter().postNotificationName("disconnected", object: self)
相关问题