2015-04-22 3 views
0

Метод выбора NSNotificationCenter никогда не вызывается.Получение проблемы в NSNotificationCenter

PostNotification, следуют:

- (IBAction)go:(id)sender { 
AnotherViewController *obj = [self.storyboard instantiateViewControllerWithIdentifier:@"AnotherViewController"]; 

[[NSNotificationCenter defaultCenter] 
postNotificationName:@"TestNotification" 
object:self]; 




[self.navigationController pushViewController:obj animated:YES]; 

} 

Наблюдайте Уведомление в AnotherViewController.m:

- (void)viewDidLoad { 
[super viewDidLoad]; 

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


// Do any additional setup after loading the view. 
    } 

и receiveTestNotification является:

- (void) receiveTestNotification:(NSNotification *) notification 
{ 


if ([[notification name] isEqualToString:@"TestNotification"]) 
    NSLog (@"Successfully received the test notification!"); 
} 

В чем проблема? Спасибо

+0

Этот код выглядит хорошо для меня. – trojanfoe

+0

, но не работает для меня. Контроль никогда не доходит до селекторного метода. –

ответ

0

Вы должны перенести метод добавления наблюдателя на инициализацию контроллера вида. Поскольку вы отправляете уведомление до того, как AnotherViewController зарегистрируется для получения уведомления.

- (instancetype)initWithCoder:(NSCoder *)aDecoder 
    { 
     self = [super initWithCoder:aDecoder]; 
     if (self) { 
      [[NSNotificationCenter defaultCenter] addObserver:self 
                selector:@selector(receiveTestNotification:) 
                 name:@"TestNotification" 
                 object:nil]; 
     } 
     return self; 
    } 
+0

он работает. Спасибо cekisakurek :) –

+0

приветствую :). было бы здорово, если бы вы приняли ответ в качестве решения. – cekisakurek

+0

iam new on stackoverflow, не знаю, как принять в качестве ответа .. скажите, что у процесса –

Смежные вопросы