NSNotificationCenter在弹出视图中不起作用(ios objC)

时间:2016-03-15 00:15:44

标签: ios objective-c nsnotificationcenter

我正在处理iPad项目(xcode 7.21 + iOS9)而NSNotificationCenter无法正常工作。

当用户打开我的应用程序时,将出现标签栏控制器。

- (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];
    ...
}

1 个答案:

答案 0 :(得分: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 Kumar评论:)) NSNotificationCenter doesn't work in popup view(ios objC)