ios - NSNotificationCenter 永远不会执行
问题描述
这是我的代码。
在这里创建调用Example
到 ViewController中的 Notification 的观察者
- (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"}];
}
推荐阅读
- python - 如果 Layout 连接到父级,为什么动态调整大小不起作用?
- angular - 如何从 HTML 以角度设置局部变量?
- python - 如何使用 for 循环构建可以从列表中读取值的字典?
- sql - SQL插入接口切片
- python - 将 TwitterAPI 结果保存到 csv 文件
- python - 返回非实例化类类型的类型提示
- android - 备份时的Android 9数据库日志模式WAL问题
- javascript - 如何在同一个 div 中使用 jquery hover 来处理 div 和 html 标签
- count - Tableau - 计算最大数量
- c++ - C++ 中是否有 '_itoa_s' 函数的替代品