首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >NSNotificationCenter不工作ios 7

NSNotificationCenter不工作ios 7
EN

Stack Overflow用户
提问于 2014-02-01 09:34:37
回答 1查看 1.7K关注 0票数 1

我正在尝试各种NSNotificationCenter示例,但它们都不起作用。我有一个UITableViewController,当用户单击一个单元格时,它打开一个容纳WebView的UIViewController。为了加快速度,我正在尝试让UITableViewController日志收到一条消息,即当带有WebView的UIViewController加载时,它会收到通知。我现在拥有的是:

在MyTableViewController中-

代码语言:javascript
复制
- (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里有一个网络视图,我有-

代码语言:javascript
复制
- (void)viewDidLoad
{
 [super viewDidLoad];

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

 ....
}

当我运行并触摸UITableviewController中的一个单元格以使用WebView打开UIViewController时,没有日志消息。没有撞车,什么都没有。怎么回事?

任何帮助都是非常感谢的。

编辑

代码语言:javascript
复制
- (id)initWithCoder:(NSCoder *)aDecoder
{

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

return self;
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-02-01 09:50:35

如果您使用的是Interface,那么initWithStyle:将不会被调用为您的初始化程序,而是重写initWithCoder:

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/21496383

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档