如何在通知中发送notification.userinfo

时间:2017-03-22 12:50:25

标签: ios swift notifications nsnotificationcenter

在Appdelegate.swift中,

func application(application: UIApplication, didReceiveLocalNotification notification: UILocalNotification) {

 NSNotificationCenter.defaultCenter().postNotificationName("notification", object: nil, **userInfo: notification.userInfo**)
}

在ViewController.swift中

我包括了观察者和方法

override func viewDidLoad() {
    super.viewDidLoad()

    NSNotificationCenter.defaultCenter().addObserver(self, selector:"handleNotification", name: "notification", object: nil)
 }

func handleNotification(notification: NSNotification){
    print("enter ")
}

当我处理通知时,它会崩溃"无法识别的选择器发送到实例" 什么是问题?

2 个答案:

答案 0 :(得分:0)

适用于Swift 3

将通知观察者更改为

NotificationCenter.default.addObserver(self, selector: #selector(handleNotification(with:)), name: notification, object: nil)

尝试将handleNotification功能更改为:

func handleNotification(with notification: Notification){
    print("enter ");
}

答案 1 :(得分:0)

请添加并检查。

NSNotificationCenter.defaultCenter().addObserver(self, selector:#selector(handleNotification(_:)), name: "notification", object: nil)