2011-10-17 25 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上调用它。 –