2014-10-06 83 views
0

我正在嘗試使用UIImagePickerControl執行presentViewController以顯示標準Camera Roll照片選取器。這在我的大部分應用程序中都是端到端的。它不起作用的地方是當我想在已經呈現的viewController內使用imagePicker的時候;無法呈現呈現相冊的視圖。presentViewController--嘗試在__上呈現__,__其視圖不在窗口層次結構中

基本思想是我試圖訪問窗口對象上的rootViewController,或者應用程序委託的持久tabBarController(這是rootViewController);這兩個例子都是希望永遠存在的頂級項目的例子。只是使用「自我」,否則最終會作爲呈現它的局部視圖。

有一個可靠的方式presentViewController內已經呈現的視圖?

dispatch_async(dispatch_get_main_queue(),^{ 
    // 1. attempt that works well elsewhere in app 
    [((AppDelegate*)[[UIApplication sharedApplication] delegate]).tabBarController presentViewController:self.imagePickerController animated:YES completion:nil]; 

    // 2. this does nothing, no crash or action (_UIAlertShimPresentingViewController) 
    [[[UIApplication sharedApplication] keyWindow].rootViewController presentViewController:self.imagePickerController animated:YES completion:nil]; 

    // 3. attempt off related internet suggestion, nothing happens 
    UIViewController *topController = [UIApplication sharedApplication].keyWindow.rootViewController; 
    while (topController.presentedViewController) { 
     topController = topController.presentedViewController; 
    } 
    [topController presentViewController:self.imagePickerController animated:YES completion:nil]; 

}); 

回答

0

因爲presentViewController意爲模式的介紹,我不認爲這是有可能在同一UIWindow同時出示第二模式。但是,您可以添加新的UIWindow並將其呈現在那裏。

originalWindow = [[[UIApplication sharedApplication] keyWindow]; 
tempWindow = [[UIWindow alloc] init]; 
UIViewController *controller = [[UIViewController alloc] init]; 
tempWindow.rootViewController = controller; 
[tempWindow makeKeyAndVisible]; 
[controller presentViewController:self.imagePickerController animated:YES completion:nil]; 

你需要添加像這樣在你的迴應圖像選擇器的返回:

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info { 
    // Process the result and then... 
    [self cleanupWindow]; 
} 

- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker { 
    [self cleanupWindow]; 
} 

- (void)cleanupWindow { 
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(madeKeyWindow:) name:UIWindowDidBecomeKeyNotification object:nil]; 
    [originalWindow makeKeyAndVisible]; 
} 

- (void)madeKeyWindow:(NSNotification *)notification { 
    [tempWindow removeFromSuperview]; 
    tempWindow = nil; 
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIWindowDidBecomeKeyNotification object:nil]; 
} 
相關問題