Sending data to a method registered for a notification - iphone

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.

Related

Why I cannot receive the Notification

[[NSNotificationCenter defaultCenter] postNotificationName:kNotificationForbidAdvPost object:nil];
I define postNotificationName:kNotificationForbidAdvPost in a .h file
Im my subclass of NSApplication, I import this .h file. And have a observer of this Notification
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(changeToRecordingStatus) name:kNotificationForbidAdvPost object:nil];
I am sure that the observer was added first, and then post the notification.
And I see the [UINotification default] in Debug. It have same memory address in two part of code.
Is there some mistake I have?
Thanks!
The documentation is pretty clear when it states that
The method specified by
notificationSelector must have one and
only one argument (an instance of
NSNotification).
http://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSNotificationCenter_Class/Reference/Reference.html#//apple_ref/doc/uid/20000219-CHDCIGGB
So, I see your selector you are registering is changeToRecordingStatus when it must be changeToRecordingStatus:
- (void) changeToRecordingStatus: (NSNotification *) notification;
I have try that add a arugument, but it is still not worked.
I create a new project and use a no-argument function to response Notification, it is worked normally.
The reason why I cannot receive the Notification is that a removeObserver method was called at one part which I didn't notice.
Thanks for all your reply.

NSNotification to be called like in a For loop

I have a parser class and a view controller class. In the parser class i am sending a request and receiving an asynchronous response. I want to download the same several times but while sending different parameters each time. I have registered an observer in the class:
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(dataDownloadComplete:) name:OP_DataComplete object:nil];
and then post a notification in :
-(void)connectionDidFinishLoading:(NSURLConnection *)
connection method of the parser class.
[[NSNotificationCenter defaultCenter] postNotificationName:OP_DataComplete object:nil];
I can't enclose the observer inside a For loop. What better alternative is there? Please help. Thanks in advance.
You probably do not need a for loop at all. As you said you send a request asynchroniously, so just send it in your observer code instead of making a for loop. Then you get back to your observer after response is downloaded.

how to use NSNotification in objective c

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/

NSNotification in iphone sdk

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.

forwarding a notification to another class

I am using this method
- (void)keyboardWillShow:(NSNotification *)notification
that is triggered when the keyboard shows.
When this method is triggered, it receives a notification that contains several parameters about the keyboard, as the animation duration, animation curve and frame. I need to forward this notification and all its parameters to another class. So, I've tried to do this inside keyboardWillShow:
[[NSNotificationCenter defaultCenter]
postNotificationName:#"doSomething" object:notification userInfo:nil];
the doSomething notification runs doSomething method on another class, and it has this form:
- (void) doSomething:(NSNotification *)notification {
but when I try to read the notification values on this other class, using, for example,
myRect = [[[notification userInfo] objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
I obtain zero for all values. The notification is losing its parameters and not being forwarded. How can I do that?
thanks.
It is losing all the values because you passed nil when you created the new notification! You must pass the keyboard notification's userInfo dictionary if you want this to work. So:
[[NSNotificationCenter defaultCenter]
postNotificationName:#"doSomething" object:self userInfo:userInfo];
In this case, the object creating this notification is this object. If you want this to appear as to come from the keyboard object, then send [notification object] for object, but I don't recommend this. Also, why not simply have the class that you want to respond to these notifications ALSO listen for keyboard notifications? Creating and sending a new notification from within the keyboard notification call-back seems kind of round-about to me. The other option is to create a delegate-style thing where the class (I am assuming a view controller or something?) actually listening for keyboard notifications then calls back a delegate object (that other class that you want to forward this info to) and passes the userInfo dictionary to it directly. So:
// Assume a proper delegate is set and it responds to - (void)doSomething:(NSDictionary*)userInfo
[delegate doSomething:userInfo]; // from within the keyboard notification
Still, I think I would just have this other class listen for keyboard notifications itself, though.