Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/101.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 通知中心swift3可以';不守岗位_Ios_Swift_Swift3_Nsnotificationcenter_Notificationcenter - Fatal编程技术网

Ios 通知中心swift3可以';不守岗位

Ios 通知中心swift3可以';不守岗位,ios,swift,swift3,nsnotificationcenter,notificationcenter,Ios,Swift,Swift3,Nsnotificationcenter,Notificationcenter,我有3个通知: NotificationCenter.default.post(name:NSNotification.Name("Notification1"), object: nil) NotificationCenter.default.post(name:NSNotification.Name("Notification2"), object: nil) NotificationCenter.default.post(name:NSNotification.Name("Notifica

我有3个通知:

NotificationCenter.default.post(name:NSNotification.Name("Notification1"), object: nil)
NotificationCenter.default.post(name:NSNotification.Name("Notification2"), object: nil)
NotificationCenter.default.post(name:NSNotification.Name("Notification3"), object: nil)
每个帖子都有一个不同的观察者在视图控制器中

首先
NotificationCenter.default.addObserver(forName:NSNotification.Name(“Notification1”),对象:nil,队列:nil,使用:updateUx)

Second
NotificationCenter.default.addObserver(forName:NSNotification.Name(“Notification2”),对象:nil,队列:nil,使用:updateUx)

Third
NotificationCenter.default.addObserver(forName:NSNotification.Name(“Notification3”),对象:nil,队列:nil,使用:updateUx)

updateUx func仅包含通知的打印


我只收到了我的第一个通知,我无法捕捉到另外两个,我不知道为什么。

添加您的观察者,如下所示:

NotificationCenter.default.addObserver(self, selector: #selector(updateUx), name: NSNotification.Name("Notification1"), object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(updateUx), name: NSNotification.Name("Notification2"), object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(updateUx), name: NSNotification.Name("Notification3"), object: nil)
你可以走了


编辑:完整的源代码(此项目在视图中有一个
ui按钮
,并且
@IBAction
在情节提要中与之连接。点击该按钮后,所有3个通知都将发布。日志应在控制台中打印三次)


添加您的观察者,如下所示:

NotificationCenter.default.addObserver(self, selector: #selector(updateUx), name: NSNotification.Name("Notification1"), object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(updateUx), name: NSNotification.Name("Notification2"), object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(updateUx), name: NSNotification.Name("Notification3"), object: nil)
你可以走了


编辑:完整的源代码(此项目在视图中有一个
ui按钮
,并且
@IBAction
在情节提要中与之连接。点击该按钮后,所有3个通知都将发布。日志应在控制台中打印三次)


你的第一次通知是不可能收到的。您没有观察到
Notification1
,但观察到的是
仪表板通知
是否确定要添加的观察者和发布观察者的名称相同?请尝试更改
NotificationCenter.default.addObserver
,如下所示。它应该会起作用。我试过了,它成功了。很抱歉,我的错,因为我在github上发布了这个,我更改了名称,编辑了我的问题。我在使用这个代码时没有遇到任何问题。您确定添加了您的观察者并发布了通知吗?无法接收您的第一次通知。您没有观察到
Notification1
,但观察到的是
仪表板通知
是否确定要添加的观察者和发布观察者的名称相同?请尝试更改
NotificationCenter.default.addObserver
,如下所示。它应该会起作用。我试过了,它成功了。很抱歉,我的错,因为我在github上发布了这个,我更改了名称,编辑了我的问题。我在使用这个代码时没有遇到任何问题。您确定添加了您的观察员,然后发布了通知吗?