[英]NSNotificationCenter won't run selector method
嗨,我有一个奇怪的问题,
在MainViewController类中,每当按下按钮时,我都会发布一个通知
[[NSNotificationCenter defaultCenter] postNotificationName:@"step" object:nil];
在课堂上,我自己有一个观察者
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(step) name:@"step" object:nil];
运行“步骤”方法
- (void)step {
NSLog(@"works");
}
在另一个名为Slide1ViewController的类中,我也有一个观察者
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
NSLog(@"works 2");
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(step) name:@"step" object:nil];
}
return self;
}
然后dealloc方法将其删除
- (void)dealloc {
[[NSNotificationCenter defaultCenter] removeObserver:self name:@"step" object:nil];
}
在MainViewController中,将调用该方法,但不会在Slide1ViewController中调用该方法。
Slide1ViewController像这样初始化:
Slide1ViewController*test = [[Slide1ViewController alloc] initWithNibName:@"Slide1ViewController" bundle:nil];
test.view.frame = CGRectMake(1024*0, 0, 1024, 768);
[contentScrollView addSubview:test.view];
在发布通知之前,您必须注册两个观察者,因此在发布通知之前,必须在内存中初始化Slide1viewcontroller。
非常感谢@Phillip Mills,您是对的,我需要将其声明为MainViewController的@property,现在它可以正常工作了!!! 非常感谢,这个问题使我发疯。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.