是否有可能performSelector:withObject:afterDelay:
在子线程中不起作用?不适用于NSThread:performSelector:withObject:afterDelay:?
我还是新来的目标C和Xcode所以也许我错过了一些明显的东西......:/我真的很感谢一些帮助。
我想要做的就是显示infolabel 3秒钟,之后它将被隐藏。如果设置了新信息,3秒后隐藏标签的线程将被取消。 (我不想通过旧线隐藏的新信息。)
源代码:
- (void) setInfoLabel: (NSString*) labelText
{
// ... update label with text ...
infoLabel.hidden = NO;
if(appDelegate.infoThread != nil) [appDelegate.infoThread cancel]; // cancel last hide-thread, if it exists
NSThread *newThread = [[NSThread alloc] initWithTarget: self selector:@selector(setInfoLabelTimer) object: nil];// create new thread
appDelegate.infoThread = newThread; // save reference
[newThread start]; // start thread
[self performSelector:@selector(testY) withObject: nil afterDelay:1.0];
}
-(void) setInfoLabelTimer
{
NSLog(@"setInfoLabelTimer");
[self performSelector:@selector(testX) withObject: nil afterDelay:1.0];
[self performSelector:@selector(hideInfoLabel) withObject: nil afterDelay:3.0];
NSLog(@"Done?");
}
-(void) testX
{
NSLog(@"testX testX testX testX testX");
}
-(void) testY
{
NSLog(@"testY testY testY testY testY");
}
-(void) hideInfoLabel
{
NSLog(@"f hideInfoLabel");
if(!([[NSThread currentThread] isCancelled])) {
AppDelegate *appDelegate = (AppDelegate *) [[UIApplication sharedApplication] delegate];
appDelegate.infoThread = nil;
appDelegate.infoLabel.hidden = YES;
[NSThread exit];
}
}
控制台产出:
- setInfoLabelTimer
- 完成?
- 暴躁易怒暴躁易怒暴躁
正如你可以看到performSelector:withObject:afterDelay:
不工作(--->“暴躁易怒暴躁易怒暴躁的”),但不是在子线程(它运行(--->“setInfoLabelTimer “和”完成?“))
有没有人知道为什么performSelector:withObject:afterDelay
:不工作在子线程? (或者有什么是我的错?:()
最好的问候, 茶壶
是的,它不工作。任何视图相关的东西都必须是主要的。 –
testx和testy方法在.h中定义? –
你不应该搞乱后台线程中的UIKit对象。 – bbum