-1

Just like the question title

for say I have code like

func receieveNotification(notification : NSNotification) {

    ......verify notification
    ......retrieve userInfo

}

Do I still need to add observer to NSNotificationCenter.defaultCenter() ? If I do. How to?

Jay Hu
  • 6,041
  • 3
  • 22
  • 33
  • how would this method would be executed otherwise. and also I would a method called `gestureHandler` expect to accept a gesture recognizer as sender. – vikingosegundo Jul 10 '15 at 19:39
  • ok, the name of the function is related to the rest of the project. If it doesn't make sense. I could change. I don't know how I would be executed, that's why I asked the question. B/C I saw some sample code like this. I assume every time NSNotificationCenter has new notification, it would pass notification to the functions that take NSNotification as parameter. – Jay Hu Jul 10 '15 at 19:44
  • sorry for answering your question with a counter question. It is just that I cannot stand question that are definitely ask because someone just is too lazy to invest a minimum in either reading documentation or simply try it out.Again: Sorry. (attention: comment may contain irony) – vikingosegundo Jul 10 '15 at 19:48

2 Answers2

0

Yes it's required.

Use it like: Snippet taken from a great tutorial: http://natashatherobot.com/ios8-where-to-remove-observer-for-nsnotification-in-swift/

class FirstViewController: UIViewController {
@IBOutlet weak var sentNotificationLabel: UILabel!

override func viewDidLoad() {
    super.viewDidLoad()
    NSNotificationCenter.defaultCenter().addObserver(self, selector: "updateNotificationSentLabel", name: mySpecialNotificationKey, object: nil)
}

// 2. Post notification using "special notification key"
@IBAction func notify() {
    NSNotificationCenter.defaultCenter().postNotificationName(mySpecialNotificationKey, object: self)
}

func updateNotificationSentLabel() {
    self.sentNotificationLabel.text = "Notification sent!"
}

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

}

Further topic: Swift and removing Observers: http://natashatherobot.com/ios8-where-to-remove-observer-for-nsnotification-in-swift/

charlyatwork
  • 1,187
  • 8
  • 13
0

An NSNotification is sent when some object calls the post method on an NSNotificationCenter. The notification center then calls the designated receiving method on each object that has been registered with it.

If you do not register with a notification center, then there is no way for the system to know that it should send the notification to you. Although there can be other registration centers, in iOS you will almost always use the default.

When you register for a notification, you specify which object is to receive the notification, what method on that object to call, which notification you are registering for, and what sender you want to receive notifications from. If you want to receive every one of a particular kind of notification (that is, you don't care which object sent it), you can specify nil for the sender.

Thus, to register for the notification, "MyNotification", and you don't care what object sent it, you call the following:

NSNotificationCenter.defaultCenter().addObserver(self, "gestureHandler", "MyNotification", nil)

Where to place this call depends on when you want this object to listen for it. For example, if the receiver is a UIView, you probably want to register when the view is about to be shown, not when the view is created.

It is extremely important that you unregister when you want to stop receiving notifications, such as when the receiver goes out of scope. You do this by calling 'removeObserver()`.

You should search through Xcode's documentation and read the Notification Programming Topics.

Matthew Burke
  • 2,295
  • 17
  • 14