I don't know how to use NSNotification in our iphone application. and one more doubt of difference between delegate and NSNotification because both are communicating through objects.
and give the practical example.
=> NSNotificationCenter provides a centralized hub through which any part of an application may notify and be notified of changes from any other part of the application.
=> Observers register with a notification center to respond to particular events with a specified action.
=> Each time an event occurs, the notification goes through its dispatch table, and messages any registered observers for that event.
Use Of NS-Notification in Objective C
//Write from where you want to pass the data
[[NSNotificationCenter defaultCenter]postNotificationName:#"TeamTable" object:hdImage userInfo:nil];
Here
**TeamTable is notification observer name (Unique name)
**hdImage is what data you want to pass to another controller
Now write these code in that Controller from where you want to receive
the data
-(void)viewWillAppear:(BOOL)animated{
[[NSNotificationCenter defaultCenter]addObserver:self selector:#selector(detailsData:) name:#"TeamTable" object:nil];
}
-(void)detailsData:(NSNotification*)sender{
//In sender it contain All received data
}
It’s important for objects to remove observers before they’re deallocated, in order to prevent further messages from being sent.
-(void)viewWillDisappear:(BOOL)animated{
[[NSNotificationCenter defaultCenter]removeObserver:self name:#"TeamTable" object:nil];
}
For More Details about NS-Notification You can follow this Link http://nshipster.com/nsnotification-and-nsnotificationcenter/
Related
I came across a piece of code that was registering a notification with:
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(someStuff:)
name:#"someStuff"
object:nil];
and then triggerring it with:
[[NSNotificationCenter defaultCenter] postNotificationName:#"someStuff" object:self];
Why is one object set to nil and other to self? What does that do?
Taking the second case first, when you post a notification you indicate which object is the sender of that notification. Usually that is 'self', but you can post a notification on some other object's behalf.
On the other hand, when you register to observe notifications, you can specify that you only want to observe notifications that are posted by a particular object. So you can say "I want to observer the 'cool new data' notification, but only if this particular object posts it. If some other object says there is 'cool new data', I'm not interested."
If you pass nil as the 'object' parameter when you register an observer, then you're saying you don't care who posts this notification, you want your selector (or block) to fire for any sender. So if anyone posts 'cool new data', I want to know about it.
BTW, these are not "push notifications". That's a different feature/API.
to quote apple docs.. the first object is
notificationSender
The object whose notifications the observer wants to receive; that is, only notifications sent by this sender are delivered to the
observer.
If you pass nil, the notification center doesn’t use a notification’s sender to decide whether to deliver it to the observer.
and the second is
notificationSender
The object posting the notification.
so in second case this tell who is actually sending this notification..that is that class itself.
I have an NSAutoreleasePool thread that is designed to pull information down from a web service, i have the web service code working nicely and i can trigger the thread to start in a view controller without any trouble, in fact its working quite nicely.
I want to:
move the thread instantiation to the appDelegate - easy!
have it run periodically and somehow tell the viewcontrollers under it (5 - 10) if new information is downloaded
have the capacity to manually execute the thread outside of the scheduler
I can fire up a method on the appdelegate using performSelectorOnMainThread but how i can get my child view controllers to "subscribe" to a method on the appdelegate?
Using NSNotificationCenter you can post well, notifications :D
That way without the appDelegate nowing the other classes the other classes can "subscribe" to the notifications they need.
Also, i would keep the thread alive, spawning a new thread everytime is costly, ofc only if it is spawned often. I would recommend using GCD ( iOS 4+ )
Here's what you do:
From the class sending the message, post a notification like :
[[NSNotificationCenter defaultCenter] postNotificationName: #"YOUR_NOTIFICATION_NAME" object: anyobjectyouwanttosendalong(can be nil)];
In the view controllers where you want to be notified of the notification when posted:
In the viewDidLoad do:
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(METHOD_YOU_WANT_TO_INVOKE_ON_NOTIFICATION_RECEIVED) name:#"YOUR_NOTIFICATION_NAME" object:sameasbefore/nil];
Important! Don't forget this in your viewDidUnload():
[[NSNotificationCenter defaultCenter] removeObserver:self name:#"YOUR_NOTIFICATION_NAME" object:sameasbefore/nil];
I'm not very sure about the object associated with notifications but you can look that up here
NOTE: When it's only one object notifying another one, you're better off using protocols :) But in this case since there are multiple view controllers listening, use notifications
Use NSNotificationCenter to send events that your view controllers are observing?
I need to pass some data to a method which I am registering to execute once I receive a notification
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(keyboardWillShow:withCell:) name:UIKeyboardWillShowNotification object:nil];
Here I want to send some data for withCell part of my method.
How can I achieve this?
Short answer: you can't. You're registering for a notification and you can't control what is sent with that notification.
What is it you would like to do?
When you're posting notification and want to pass some data use:
- (void)postNotificationName:(NSString *)notificationName object:(id)notificationSender userInfo:(NSDictionary *)userInfo
but this is system notification in your case, so the best choice for you would be registering callback in your view controller that would be called when keyboard is shown (it receives only one parameter - NSNotification). You'll have to use some ivar (e.g. selectedCell) and process it in that callback.
I have an application where I used a UITabBarController with 3 buttons. So I also have 3 classes. What I want to do is to call an - (IBAction) doSomething: (id) sender {} in class 1 (view 1) with a button in class 2 (view 2).
Take whatever it is that your doSomething method (not function) does and use it to create method in a new class. Both controllers can import the class, instantiate it, and use the method.
Alternately you can send a notification to whichever controller has doSomething, but if the code in the method really does apply to both controllers, provide it to both controllers.
You can have one controller send a notification to another. When you want to notify class 1 to perform the button-pressed code you'll send out a notification like this:
[[NSNotificationCenter defaultCenter] postNotificationName:#"ABCPerformButtonAction"
object:nil];
You don't have to call it ABCPerformButtonAction, you just need a string that you'll recognize and something -- I used ABC because I don't know your initials or the name of the app or whatever -- to help ensure you don't accidentally send a notification that has the same name as a notification something you're unaware of is listening for (including 3rd party libraries you're using, etc.).
When that notification goes out, any object that has registered with the defaultCenter to listen for #"ABCPerformButtonAction" will perform any actions you choose. Here's how controller 1 registers (this should be located in some place like ViewDidLoad or the initialization method of the object):
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(performDoSomething:)
name:#"ABCPerformButtonAction"
object:nil];
The selector there, performDoSomething:, is just the name of a method you want to run when the notification goes out. That method has to have a specific format, so you can't call your doSomething method directly. It would look like this:
- (void)performDoSomething:(NSNotification *)notif {
[self doSomething];
}
As you can see, all it does is call the method. Obviously it could do much more, and you can even send information along with the notification (see below).
Lastly, it's important that you also remove your object as an observer before it's deallocated. In your Dealloc method of each object that registered to receive the notification you add this:
[[NSNotificationCenter defaultCenter] removeObserver:self];
Hopefully that makes sense. The Apple documentation for NSNotificationCenter explains more and they provide several sample apps that use notifications.
what is the use of NSNotification in iphone sdk?
Thanks
I know this isn't generally a good way to answer questions, but RTFM.
NSNotification objects encapsulate information so that it can be broadcast to other objects by an NSNotificationCenter object. An NSNotification object (referred to as a notification) contains a name, an object, and an optional dictionary. The name is a tag identifying the notification. The object is any object that the poster of the notification wants to send to observers of that notification (typically, it is the object that posted the notification). The dictionary stores other related objects, if any. NSNotification objects are immutable objects.
You can create a notification object with the class methods notificationWithName:object: or notificationWithName:object:userInfo:. However, you don’t usually create your own notifications directly. The NSNotificationCenter methods postNotificationName:object: and postNotificationName:object:userInfo: allow you to conveniently post a notification without creating it first.
NSNotifications allow you to have a method called when a event occurs.
For example if you have a MPMoviePlayer and you want todo something when it is done you could use the following code:
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(movieFinished:) name:MPMoviePlayerPlaybackDidFinishNotification object:yourMoviePlayer.moviePlayer];
or if you want to do something when the device rotates:
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(orientationChanged:) name:UIDeviceOrientationDidChangeNotification object:self];
You should have a look at the Notifications section in the Cocoa Fundamentals Guide. There's anything you need to know about notifications there : from definition to use cases.
In terms of events NSNotifications are an alternative to delegation. Delegation can be used to notify one single delegate of an event, whereas notifications can be used to notify an arbitrary number of receivers. A notification is sent to the main notification center, which then notifies every object, that has registered for the notification.
One important difference is, that with delegation you can receive the delegates response to the event, whereas with NSNotifications you just send away the notification, but you don't know about the receivers or their response to the notification.
You register a UINotification when you want to receive a alert from iOS. So if you want to do something when a accessory is plugged in or a TV is plugged in you would register a UINotification for it and it would call a method in your app when the event occurs.