gpt4 book ai didi

iphone - NSNotificationCenter 不对消息起作用

转载 作者:行者123 更新时间:2023-11-29 11:18:54 27 4
gpt4 key购买 nike

在我的一个类(class)中,我发布了一条通知:

[[NSNotificationCenter defaultCenter] postNotificationName:@"ALERTNOTI" object:self userInfo:nil];

在我的应用委托(delegate)中,我会听:

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

我使用 NSLog 来跟踪我何时发送以及何时调用方法 myMethod

尽管我发送了通知,但该方法没有被调用。

关于 NSNotification 有什么我需要知道的吗?脾气暴躁吗?

最佳答案

 do this changes

[[NSNotificationCenter defaultCenter] postNotificationName:@"ALERTNOTI" object:nil];

if you want to pass some object through the notification. then do this
ex: you want to pass an NSDictionary *dict

[[NSNotificationCenter defaultCenter] postNotificationName:@"ALERTNOTI" object:nil userInfo:dict];

你想通过通知调用的方法应该是这样的。

  -(void)method:(NSNotification *) notif
{
// your code here.

//if you want to access your dict
NSDictionary *myDict=[notif userInfo];



}

关于iphone - NSNotificationCenter 不对消息起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8166118/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com