Reputation: 4699
I am developing my very first app for iOS and not understanding notifications.
I am sending notifications as:
DefaultCenter.postNotificationName("evRodadaAtualizei", object: nil)
In another class, I have a method that observes this notification:
DefaultCenter.addObserver(self, selector: Selector("Atualizar"),
name: "evRodadaAtualizei", object: nil)
My question is:
This observer will listen any notification with that name? It is not important the class where notification was declared? In other words, is possible to have a place to put all the notifications (like a notification library), because all of them are independent of the class?
If I am understanding correctly, this is very different of the concept of events in C# or VB.Net where events belongs to classes.
Upvotes: 1
Views: 477
Reputation: 53111
Notifications in Cocoa work inter-class. It doesn't matter where the notification is created or observed.
However, note the object
parameter on the postNotificationName
method. If set, this should correspond to the object posting the notification. If you only want to observe notifications for a given object, set the object parameter to that object when you add the observer. e.g.
class MyObjectClass {
func doSomething() {
// Do something and then notify
DefaultCenter.postNotificationName("evRodadaAtualizei", object: self)
}
}
class MyObserverClass {
func startProcess() {
var myObject = MyObjectClass()
DefaultCenter.addObserver(self, selector: Selector("Atualizar"), name: "evRodadaAtualizei", object: myObject)
}
func Atualizar() {
}
}
Upvotes: 3