簡體   English   中英

NSNotificationCenter在彈出視圖中不起作用(iOS objC)

[英]NSNotificationCenter doesn't work in popup view(ios objC)

我正在開發一個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];
    ...
}

首先:

- (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在彈出視圖中不起作用(ios objC)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM