Swift:Notification Observer Using NSNotificationCenter

From Hawk Wiki
Revision as of 04:11, 23 March 2015 by Hall (Talk | contribs)

Jump to: navigation, search

Listen Notification in Wwift

func tweetSent(notification: NSNotification) {
    let newTweet:Dictionary<String, Tweet> = notification.userInfo as Dictionary<String, Tweet>
}
NSNotificationCenter.defaultCenter().addObserver(self, selector: "tweetSent:", name: "newTweetNotification", object: nil)

Send/Post Notification in Wwift

var userInfo: Dictionary<String, Tweet> = ["tweet": newTweet]
NSNotificationCenter.defaultCenter().postNotificationName("newTweetNotification", object: nil, userInfo: userInfo)

Remove Notification Bbserver

deinit {
    NSNotificationCenter.defaultCenter().removeObserver(self)
}