gpt4 book ai didi

ios - NSNotificationCenter无法正常工作的iOS 7

转载 作者:行者123 更新时间:2023-12-01 19:03:17 25 4
gpt4 key购买 nike

我正在尝试各种NSNotificationCenter的示例,但是都没有用。我有一个UITableViewController,当用户单击一个单元格时,它将打开一个包含WebView的UIViewController。只是为了使事情顺利进行,我试图让UITableViewController记录一条消息,当带有WebView的UIViewController加载时,它收到了通知。这是我所拥有的:

在MyTableViewController中-

- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {

[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(receiveTestNotification:)
name:@"TestNotification"
object:nil];
}
return self;
}


- (void) receiveTestNotification:(NSNotification *) notification
{

if ([[notification name] isEqualToString:@"TestNotification"]) {
NSLog (@"Successfully received the test notification!");
}

}

- (void) dealloc
{

[[NSNotificationCenter defaultCenter] removeObserver:self];
}

然后在我的具有Webview的UIViewController中,我有-
- (void)viewDidLoad
{
[super viewDidLoad];

[[NSNotificationCenter defaultCenter]
postNotificationName:@"TestNotification"
object:self];

....
}

当我运行并触摸UITableviewController中的单元格以使用WebView打开UIViewController时,没有日志消息。没有崩溃,什么都没有。是什么赋予了?

任何帮助深表感谢。

编辑
- (id)initWithCoder:(NSCoder *)aDecoder
{

self = [super initWithCoder:aDecoder];
if (self)
{
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(receiveTestNotification:)
name:@"TestNotification:"
object:nil];
}

return self;
}

最佳答案

如果您使用的是Interface Builder,则initWithStyle:不会作为初始化程序调用,而是覆盖initWithCoder:

关于ios - NSNotificationCenter无法正常工作的iOS 7,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21496383/

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