定时选择器从未执行过

时间:2010-12-22 21:19:40

标签: iphone objective-c cocoa selector

我为我的方法添加了一个观察者:

[[NSNotificationCenter defaultCenter] addObserver:self 
                                         selector:@selector(closeViewAfterUpdating) 
                                             name:@"labelUpdatedShouldReturn" 
                                           object:nil];

然后我的相关方法:

-(void)closeViewAfterUpdating; {
NSLog(@"Part 1 called");
[self performSelector:@selector(closeViewAfterUpdating2) withObject:nil afterDelay:2.0];
}

-(void)closeViewAfterUpdating2; {
NSLog(@"Part 2 called");
[self dismissModalViewControllerAnimated:YES];
}

我将这个方法分成两部分的唯一原因是我可以在触发方法之前有一个延迟。

问题是,永远不会调用第二种方法。我的NSLog输出显示Part 1 called,但它从未触发第2部分。任何想法?

编辑:我正在通过后台主题调用通知,这有什么用呢?

以下是我创建后台主题的方法:

[NSThread detachNewThreadSelector:@selector(getWeather) toTarget:self withObject:nil];

并在getWeather我有:

[[NSNotificationCenter defaultCenter] postNotificationName:@"updateZipLabel" object:textfield.text];

另外,请致电:

[self performSelector:@selector(closeViewAfterUpdating2) withObject:nil];

工作。

EDITx2:我修好了。只需要在我的主线程中发布通知,它就可以正常工作。

3 个答案:

答案 0 :(得分:1)

后台线程是问题所在。它有一个非运行的运行循环,因此永远不会调用选择器。只需让线程的NSRunLoop或CFRunLoopRef对象运行,而不会触发选择器。

答案 1 :(得分:0)

我尝试了你的代码,它在我这边工作得很好。你可能会在后台做一些时髦的事情来打断你的选择器。

答案 2 :(得分:0)

方法定义中有一个分号:

-(void)closeViewAfterUpdating2; {

是否存在代码或复制/粘贴问题?这就是为什么你永远不会看到它的问题。