Alick Dikan
Alick Dikan

Reputation: 65

nsnotification trouble

So I have a method:

-(void)didLoginWithAccount(MyAccount *)account

And I added an observer to this method like

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didLoginWithAccount:)];

And my question is, when I post Notification, how can I pass a MyAccount object?

Upvotes: 0

Views: 48

Answers (1)

dalton_c
dalton_c

Reputation: 7171

When you get a notification callback, the notification object is passed, and not the object explicitly.

Step 1, Register:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didLoginWithAccount:) name:@"MyCustomNotification" object:nil];

Step 2, Post:

[[NSNotificationCenter defaultCenter] postNotificationName:@"MyCustomNotification" object:myAccount]; 

Step 3, Recieve:

- (void)didLoginWithAccount:(NSNotification *)notification {
    MyAccount *myAccount = (MyAccount *)[notification object];
}

Upvotes: 1

Related Questions