【问题标题】:Notification does not work in Presentviewcontroller objective-c通知在 Presentviewcontroller 目标-c 中不起作用
【发布时间】:2019-06-06 13:14:58
【问题描述】:

当我收到来自 API 的响应时,我正在显示 UIAlertController。响应后,我想调用其他视图控制器的通知。

我在视图控制器 1 中添加了观察者,如下所示:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(UpdateAthleteDictionaryNotification:) name:@"UpdateAthleteDictionary" object:self.dictProfile];

在视图控制器 2 中,我的代码如下:

UIAlertController * alert = [UIAlertController
                                 alertControllerWithTitle:applicationName
                                 message:[jsonObject valueForKey:@"responseMessage"]
                                 preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction* yesButton = [UIAlertAction
                                actionWithTitle:@"Ok"
                                style:UIAlertActionStyleDefault
                                handler:^(UIAlertAction * action) {
                                    dispatch_async(dispatch_get_main_queue(), ^{
                                        [[NSNotificationCenter defaultCenter] postNotificationName:@"UpdateAthleteDictionary" object:self.dictProfile];
                                    });
                                    [self.navigationController popViewControllerAnimated:YES];
                                }];
    [alert addAction:yesButton];
    [self presentViewController:alert animated:YES completion:nil];

但它不调用。所以请你给我建议解决方案。

【问题讨论】:

  • 您是否为所有通知添加了删除观察者代码
  • 不,如果我删除,那么它将如何从另一个视图调用?
  • 这段代码在我的 Mac 上运行良好。你能提供UpdateAthleteDictionaryNotification的实现吗? self.dictProfile怎么修改?

标签: objective-c iphone notifications presentviewcontroller


【解决方案1】:

NSNotificationCenter 仅在名称和对象相同的情况下才会发送您的通知 (source)。 如果您想接收附加任何对象的通知,您应该将 nil 作为对象参数传递,在您的情况下:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-10-25
    • 1970-01-01
    • 2016-01-31
    • 1970-01-01
    • 1970-01-01
    • 2015-10-14
    • 1970-01-01
    相关资源
    最近更新 更多