2012-06-22 132 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

相關問題