如何访问NSNotification swift的标题文本属性

时间:2016-05-19 12:50:55

标签: swift nsnotificationcenter nsnotification

是否有一种简单的方法可以访问NSNotification中发送的警报标题文本? (此示例来自通知操作)

这是JSON通知

// handle the action once pressed in the notification
    func application(application: UIApplication,
                    handleActionWithIdentifier identifier: String?,
                    forRemoteNotification userInfo: [NSObject : AnyObject],
                    completionHandler: () -> Void) {

       let string = userInfo["string"] as! String

        if identifier == "VIEW_IDENTIFIER" {
            NSNotificationCenter.defaultCenter().postNotificationName("NotificationActionPressed", object: nil, userInfo:["string": "\(string)"])
       // userInfo can be accessed from any VC observing the notification
        }
        completionHandler() //dont forget this!  
    }

以下是App委托中向NSNotification中心发布通知的方法

// check for saved notifications
        NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(HomeViewController.showString(_:)), name: "NotificationActionPressed", object: nil)

以下是我的HomeVC中ViewDidLoad中的观察者

var string: String!

func showString(notification: NSNotification) {

let userInfo:Dictionary<String,String!> = notification.userInfo as! Dictionary<String,String!>
string = userInfo["string"]
// now do something else e.g show an alert.
}

这是观察者观察时调用的show方法。

<meta name="user/config/environment" content="%7B%22modulePre.your.config">

0 个答案:

没有答案