Omer
Omer

Reputation: 5600

Is it necessary to receive a NSNotification when adding an observer?

Here's is the thing..

Every time I see an example around the web related iphone - ipad dev, I see that every time that a controller register itself for a notification, the callback method is like:

-(void)mymethod:(NSNotification *)notification {
    //Bla Bla
}

the same with buttons actions.. always like:

- (void)actionmethod:(id)sender {
     //Bla Bla
}

I just make some tests, and the method is called anyway with or without the parameter. Is this really necessary? For what reason?

Thanks you !!!

Upvotes: 2

Views: 241

Answers (3)

AechoLiu
AechoLiu

Reputation: 18428

According the apple document. The button's receiver in iOS can be -(void)action, -(void)action:(id)sender. And the notification can also have a parameter NSNotification or no parameter. It depends on your needs.

For example, I need to transmit a UIImage by notification, so I will add the UIImage to the userInfo dictionary of NSNotification. As the same as the sender, if I need some properties from sender, than I will add the (id)sender as a parameter. Generally, I like to add those parameter for -(id)sender or NSNotification. If I need some information from notification or sender, then I don't need to change the original methods because they already have sender or notification objects to obtain what I need.

Update The NSNotification paramter must be added. The reference document link.

Upvotes: 0

jscs
jscs

Reputation: 64022

From the NSNotificationCenter doc:

notificationSelector
Selector that specifies the message the receiver sends notificationObserver to notify it of the notification posting. The method specified by notificationSelector must have one and only one argument (an instance of NSNotification).

[Emphasis mine.]

You must provide a selector with the correct signature; if you don't, it may work, but it may stop working just when you don't want it to.

The reason that you might want to get the notification is so that you can pass along information, in the form of the userInfo dictionary that you can specify when you yourself post a notification using notificationWithName:object:userInfo:. You can ignore the argument when the method is called, but the parameter has to be there in the method signature.

As for buttons and their actions, the docs say you can have one of three signatures*:

the UIKit framework allows three different forms of action selector:
- (void)action
- (void)action:(id)sender
- (void)action:(id)sender forEvent:(UIEvent *)event

The IBAction return type is equivalent to void (there is no return value), except that its presence in a method in your header allows Interface Builder to know that the method is intended as an action, so that you can hook up controls.

The reasons for wanting to get the button (or other control) are similar to that of notifications. In case you have many buttons in your interface, some of which (such as in a table view) connect to the same action, you may need to distinguish the exact things that you do by the pressed button's identity.


*: For completeness' sake, I want to mention that this is not the case on Mac; there, an action method must have the form: - (IBAction)action:(id)sender.

Upvotes: 3

HG's
HG's

Reputation: 828

When I found out that I can call these methods without any arguments, I did only that. It worked fine for most of the cases but in some cases it showed an NSInvalidArgumentException. It is better to mention those arguments just to be on the safe side.

Here's an example where not using arguments made the app crash :

MultipleControllers in one view

Upvotes: 1

Related Questions