gpt4 book ai didi

ios - NSNotificationCenter 永远不会被执行

转载 作者:行者123 更新时间:2023-11-29 05:51:56 26 4
gpt4 key购买 nike

这是我的代码。

这里在 ViewController 中创建名为 Example 的通知观察者

- (void)addObserverExample
{
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(example:)
name:@"Example"
object:nil];
}


- (void)example:(NSNotification *)notification{
NSLog(@"Example!!!");
}

viewDidLoad注册我的观察者

- (void)viewDidLoad
{
[self addObserverExample];
}

在我的第二个 ViewController 中。当点击按钮时执行此代码:

[[NSNotificationCenter defaultCenter] postNotificationName:@"Example" object:self.dictKeys userInfo:nil];

我遇到的问题是通知从未执行。

任何想法。

最佳答案

已根据您的问题为 NSNotificationCenter 创建了演示,它对我来说工作正常。这是该代码的链接:NSNotificationCenter Demo

- (void)viewDidLoad {
[super viewDidLoad];

[self addObserverExample];
}

- (void)addObserverExample
{
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(example:)
name:@"Example"
object:nil];
}

- (void)example:(NSNotification *)notification{
NSLog(@"Example!!!");
NSLog(@"%@",notification.userInfo);
}

- (IBAction)btnFireNotification:(id)sender {
[[NSNotificationCenter defaultCenter] postNotificationName:@"Example" object:nil userInfo:@{@"key" : @"value"}];
}

关于ios - NSNotificationCenter 永远不会被执行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55584690/

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