Difference between revisions of "Swift:Notification Observer Using NSNotificationCenter"

From Hawk Wiki
Jump to: navigation, search
(Created page with "Listen notification in swift <pre class="brush:swift"> func tweetSent(notification: NSNotification) { let newTweet:Dictionary<String, Tweet> = notification.userInfo as Dic...")
 
Line 1: Line 1:
Listen notification in swift
+
==Listen Notification in Wwift==
 
<pre class="brush:swift">
 
<pre class="brush:swift">
 
func tweetSent(notification: NSNotification) {
 
func tweetSent(notification: NSNotification) {
Line 6: Line 6:
 
NSNotificationCenter.defaultCenter().addObserver(self, selector: "tweetSent:", name: "newTweetNotification", object: nil)
 
NSNotificationCenter.defaultCenter().addObserver(self, selector: "tweetSent:", name: "newTweetNotification", object: nil)
 
</pre>
 
</pre>
Send/Post notification in swift
+
==Send/Post Notification in Wwift==
 
<pre class="brush:swift">
 
<pre class="brush:swift">
 
var userInfo: Dictionary<String, Tweet> = ["tweet": newTweet]
 
var userInfo: Dictionary<String, Tweet> = ["tweet": newTweet]
 
NSNotificationCenter.defaultCenter().postNotificationName("newTweetNotification", object: nil, userInfo: userInfo)
 
NSNotificationCenter.defaultCenter().postNotificationName("newTweetNotification", object: nil, userInfo: userInfo)
 
</pre>
 
</pre>
Remove notification observer
+
==Remove Notification Bbserver==
 
<pre class="brush:swift">
 
<pre class="brush:swift">
 
deinit {
 
deinit {

Revision as of 04:11, 23 March 2015

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)
}