2013-04-05 2 views
0

Так у меня есть метод:nsnotification неприятности

-(void)didLoginWithAccount(MyAccount *)account 

И я добавил наблюдатель к этому методу как

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

И мой вопрос, когда я отправляю уведомление, как я могу передать MyAccount объект?

ответ

1

Когда вы получаете обратный вызов уведомления, объект уведомления передается, а не объект явно.

Шаг 1, Регистрация:

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

Шаг 2, сообщение:

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

Шаг 3, ПОЛУЧИТЕ:

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