2011-10-17 26 views
0

在我的多選項卡應用程序中,一個線程關心偵聽來自服務器的消息,它必須能夠在收到特定消息時相機處於活動狀態時關閉UIImagePickerController。該線程通過NSThread detachNewThreadSelector運行。從不同線程關閉UIImagePickerController

我設法調用通過ApplicationDelegate上線側調用正確的順序,但選擇器沒有被駁回:

- (void) closeCameraController { 
    [cameraTabController closeSubViewCamera]; // Invokes cancel: on the cameraSubView. 
} 

同一序列正常工作,當我通過映射到一個按鈕的事件啓動照相機覆蓋的(一個 '取消' 按鈕):

- (IBAction) cancel { 
    [[self parentViewController] dismissModalViewControllerAnimated:NO];   
} 
+0

在大多數情況下,UIKit類不是線程安全的,他們只能從主線程中使用。在後臺執行的長時間運行的任務通常會調用[performSelectorOnMainThread:withObject:waitUntilDone:](http://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/nsobject_Class/Reference/Reference.html #// apple_ref/occ/instm/NSObject/performSelectorOnMainThread:withObject:waitUntilDone :)更新UI。 – albertamg

回答

1

Apple's Docs

注意:大多數情況下,UIKit類只能在 應用程序的主線程中使用。對於從UIResponder派生的類 或者涉及以任何方式操縱應用程序的用戶界面的情況尤其如此。

您可以使用NSObject中的performSelectorOnMainThread:withObject:waitUntilDone:

- (void) closeCameraController { 
    [cameraTabController performSelectorOnMainThread:@selector(closeSubViewCamera) 
      withObject:nil 
     waitUntilDone:YES]; 
} 
+0

謝謝,我最終解決了這個問題,使用類似於你的建議,但在appDelegate上調用它。 –