NotificationCenter 观察员没有收到帖子
NotificationCenter observers are not receiving posts
我已经尝试了好几个小时让观察者收到通知 post,但每次尝试都失败了。我将观察者放入我的根视图控制器中,它是一个嵌入式选项卡控制器,并试图让其中一个选项卡控制器发送 post 以供其接收,但无济于事。我这辈子都弄不明白,我真的很想弄明白,因为通知可能非常有用!
这是我的代码:
ViewController
let myNotification = Notification.Name(rawValue:"categoryChange")
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
override func viewDidLoad() {
super.viewDidLoad()
let nc = NotificationCenter.default
nc.addObserver(forName:myNotification, object:"test", queue:nil, using:test)
}
func test(notification:Notification) -> Void {
print("Yo")
}
这是我的 DashboardController:
override func viewDidLoad() {
super.viewDidLoad()
let myNotification = Notification.Name(rawValue:"categoryChange")
let nc = NotificationCenter.default
nc.post(name:myNotification,
object: nil,
userInfo:["message":"Hello there!"])
}
我曾尝试将观察者放入 init、viewDidLoad
和 viewDidAppear
,而 post 位于 viewDidLoad
,反之亦然。到目前为止没有任何效果,我也不知道为什么。
在此先感谢任何可以提供帮助的人!
它在我的项目中有效。
Post通知
let dic: [String:AnyObject] = ["news_id": 1 as AnyObject,"language_id" : 2 as AnyObject]
NotificationCenter.default.post(name: NSNotification.Name(rawValue: "NotificationKeyIndentifier"), object: dic)
通知观察者
override func viewDidLoad() {
super.viewDidLoad()
NotificationCenter.default.addObserver(self, selector: #selector(self.handlePushNotification(notification:)), name: NSNotification.Name(rawValue: "NotificationKeyIndentifier"), object: nil)
}
func handlePushNotification(notification: NSNotification){
if let dic = notification.object as? [String: AnyObject]{
if let language_id = dic["language_id"] as? Int{
if let news_id = dic["news_id"] as? Int{
print(language_id)
print(news_id)
}
}
}
}
您的问题是当您在 NotificationCenter
.
中添加观察者时,您使用 object
参数传递 test
(如 String
)
(来自文档)对象:
The object whose notifications the observer wants to receive; that is, only notifications sent by this sender are delivered to the observer.
If you pass nil, the notification center doesn’t use a notification’s sender to decide whether to deliver it to the observer.
这就是您的通知未被调用的原因,因为您没有借助 object
发布通知,只需将 object
设置为 nil
即可.
nc.addObserver(forName:myNotification, object:nil, queue:nil, using:test)
我已经尝试了好几个小时让观察者收到通知 post,但每次尝试都失败了。我将观察者放入我的根视图控制器中,它是一个嵌入式选项卡控制器,并试图让其中一个选项卡控制器发送 post 以供其接收,但无济于事。我这辈子都弄不明白,我真的很想弄明白,因为通知可能非常有用!
这是我的代码:
ViewController
let myNotification = Notification.Name(rawValue:"categoryChange")
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
override func viewDidLoad() {
super.viewDidLoad()
let nc = NotificationCenter.default
nc.addObserver(forName:myNotification, object:"test", queue:nil, using:test)
}
func test(notification:Notification) -> Void {
print("Yo")
}
这是我的 DashboardController:
override func viewDidLoad() {
super.viewDidLoad()
let myNotification = Notification.Name(rawValue:"categoryChange")
let nc = NotificationCenter.default
nc.post(name:myNotification,
object: nil,
userInfo:["message":"Hello there!"])
}
我曾尝试将观察者放入 init、viewDidLoad
和 viewDidAppear
,而 post 位于 viewDidLoad
,反之亦然。到目前为止没有任何效果,我也不知道为什么。
在此先感谢任何可以提供帮助的人!
它在我的项目中有效。
Post通知
let dic: [String:AnyObject] = ["news_id": 1 as AnyObject,"language_id" : 2 as AnyObject]
NotificationCenter.default.post(name: NSNotification.Name(rawValue: "NotificationKeyIndentifier"), object: dic)
通知观察者
override func viewDidLoad() {
super.viewDidLoad()
NotificationCenter.default.addObserver(self, selector: #selector(self.handlePushNotification(notification:)), name: NSNotification.Name(rawValue: "NotificationKeyIndentifier"), object: nil)
}
func handlePushNotification(notification: NSNotification){
if let dic = notification.object as? [String: AnyObject]{
if let language_id = dic["language_id"] as? Int{
if let news_id = dic["news_id"] as? Int{
print(language_id)
print(news_id)
}
}
}
}
您的问题是当您在 NotificationCenter
.
object
参数传递 test
(如 String
)
(来自文档)对象:
The object whose notifications the observer wants to receive; that is, only notifications sent by this sender are delivered to the observer.
If you pass nil, the notification center doesn’t use a notification’s sender to decide whether to deliver it to the observer.
这就是您的通知未被调用的原因,因为您没有借助 object
发布通知,只需将 object
设置为 nil
即可.
nc.addObserver(forName:myNotification, object:nil, queue:nil, using:test)