MapMapM 2013-03-11 05:23 采纳率: 0%
浏览 976

if条件语句中检测NSNotification

在发送NSNotification之后运行下面代码。但是不知道怎么讲NSNotification 放到if语句中:

if (!self.subViewControllerProv) {
    self.subViewControllerProv = [[ProvViewController alloc] init];
}
[[NSNotificationCenter defaultCenter] addObserver:self
    selector:@selector(handleNotification:) name:@"SomeNotification" object:nil];

实现:如果检查到NSNotification那么alloc 和init启动ProvViewController,如果没检测到,那么就不执行。

  • 写回答

1条回答 默认 最新

  • ChatGPTᴼᴾᴱᴺᴬᴵ 2022-12-19 21:06
    关注

    ChatGPT尝试为您解答,仅供参考
    你可以在发送 NSNotification 之后添加一个条件判断,检查是否接收到了该通知。例如:

    // 发送通知
    [[NSNotificationCenter defaultCenter] postNotificationName:@"SomeNotification" object:nil];
    
    // 在接收通知后判断
    if ([[NSNotificationCenter defaultCenter] hasReceivedNotification:@"SomeNotification"]) {
        // 如果收到了通知,则执行 alloc 和 init
        if (!self.subViewControllerProv) {
            self.subViewControllerProv = [[ProvViewController alloc] init];
        }
    }
    

    如果你想要在接收到通知后立即执行某些操作,你可以使用 addObserver:selector:name:object: 方法将通知观察者添加到 NSNotificationCenter,然后实现一个回调方法来响应通知。例如:

    // 添加通知观察者
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(handleNotification:)
                                                 name:@"SomeNotification"
                                               object:nil];
    
    // 实现回调方法
    - (void)handleNotification:(NSNotification *)notification {
        if (!self.subViewControllerProv) {
            self.subViewControllerProv = [[ProvViewController alloc] init];
        }
    }
    

    在回调方法中,你可以在接收到通知后立即执行你想要的任何操作。

    评论

报告相同问题?

悬赏问题

  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler
  • ¥15 oracle集群安装出bug
  • ¥15 关于#python#的问题:自动化测试
  • ¥20 问题请教!vue项目关于Nginx配置nonce安全策略的问题