我正在尝试各种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];
}在我的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;
}发布于 2014-02-01 09:50:35
如果您使用的是Interface,那么initWithStyle:将不会被调用为您的初始化程序,而是重写initWithCoder:
https://stackoverflow.com/questions/21496383
复制相似问题