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 树莓派与pix飞控通信
  • ¥15 自动转发微信群信息到另外一个微信群
  • ¥15 outlook无法配置成功
  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题