2016-03-15 45 views
0

我正在開發一個iPad項目(xcode 7.21 + iOS9),並且NSNotificationCenter不起作用。NSNotificationCenter在彈出視圖中不起作用(ios objC)

當用戶打開我的應用程序時,標籤欄控制器將出現。

- (void)viewWillAppear:(BOOL)animated { 
    if (false == [[MyClass sharedData] getLoginStatus]) 
    { 
     LoginViewController *loginViewController = [[UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil] instantiateViewControllerWithIdentifier:@"myCustomPopoverLoginVC"]; 
      loginViewController.modalPresentationStyle = UIModalPresentationFormSheet; 
      [self presentViewController:loginViewController animated:YES completion:^{ 

     }]; 
... 
    } 
} 

- (void)viewDidLoad 
{ 
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(notificationLogin:) name:@"afterLogin" object:nil]; 
    [super viewDidLoad]; 
... 
} 

-(void)notificationLogin:(NSNotification *)notification{ 
    NSLog(@"OhOhOh"); 
} 

在我loginView,

-(IBAction)login:(id)sender{ 
    ... 
    [[NSNotificationCenter defaultCenter] postNotificationName:@"afterLogin" object:nil]; 
    ... 
} 
+0

定義「不起作用」。 – rmaddy

+0

您確定在調用此視圖控制器的'viewDidLoad'方法後調用'postNotification'嗎? – rmaddy

+0

謝謝rmaddy.I 100%肯定postNotification被稱爲,但我沒有在控制檯中看到「OhOhOh」。 – Miken

回答

0

首先:

- (void)viewWillAppear:(BOOL)animated { 
    if (false == [[MyClass sharedData] getLoginStatus]) 
    { 
     LoginViewController *loginViewController = [[UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil] instantiateViewControllerWithIdentifier:@"myCustomPopoverLoginVC"]; 
      loginViewController.modalPresentationStyle = UIModalPresentationFormSheet; 
      [self presentViewController:loginViewController animated:YES completion:^{ 

     }]; 
... 
    } 
} 

- (void)viewDidLoad 
{ 
    // Add log here to check when its called 
    -------> NSLog("Add Observer"); 
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(notificationLogin:) name:@"afterLogin" object:nil]; 
    [super viewDidLoad]; 
... 
} 

-(void)notificationLogin:(NSNotification *)notification{ 
    NSLog(@"OhOhOh"); 
} 

然後在這裏添加另一個日誌:

-(IBAction)login:(id)sender{ 
    ... 
    -------> NSLog("Post notification"); 
    [[NSNotificationCenter defaultCenter] postNotificationName:@"afterLogin" object:nil]; 
    ... 
} 

所以,你可以查閱一下所謂的第一。然後參考@Sandeep庫馬爾評論:)) NSNotificationCenter doesn't work in popup view(ios objC)

+1

把一個NSLog裏面的「viewDidLoad」,然後另一個NSLog裏面「(IBAction)登錄:(id)發件人」。那麼你肯定會確定在通知被解僱之前觀察者是否被註冊了。 –