Sending data to a method registered for a notification

146 Views Asked by At

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?

2

There are 2 best solutions below

1
On BEST ANSWER

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?

0
On

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.