2012-06-22 136 views
1

我有这样的代码如何在特定线程完成其工作后执行一些代码?

- (IBAction)onClick:(id)sender { 

    NSThread *thread = [[NSThread alloc]initWithTarget:parser selector:@selector(adapter:) object:ph]; 

    [thread start]; 
} 

如何线程执行选择我过去之后,我可以做一些事情(显示日志或水木清华)?

回答

2

不幸的是,没有使用GCD(这是您可能无法支持的iOS 4.x功能),这个问题没有简单的答案,所以使用NSNotificationCenter并在方法为完成。

- (IBAction)onClick:(id)sender { 

    NSThread *thread = [[NSThread alloc]initWithTarget:parser selector:@selector(adapter:) object:ph]; 

    [thread start]; 
} 

-(void)adapter:(NSObject*)arg { 
    //... process and such 
    [[NSNotificationCenter defaultCenter]postNotification:[NSNotification notificationWithName:@"Thread_Done" object:nil]]; 

} 
+0

我可以看到如何通过通知,但我不知道如何执行一些 - (void)onPostExecute方法后线程完成 - (void)适配器。 – haawa

+0

这很容易,用这个替换通知:'[self performSelectorOnMainThread:@selector(onPostExecute)];' – CodaFi

相关问题