presentViewController--尝试在__上显示__,其视图不在窗口层次结构中

时间:2014-10-06 20:14:16

标签: ios presentviewcontroller

我尝试使用presentViewController进行UIImagePickerControl以显示标准的相机胶卷照片选取器。这在我的大多数应用程序中都是端到端的。它不起作用的地方是我想在已经呈现的viewController中使用imagePicker;无法呈现呈现专辑的视图。

我的基本想法是尝试访问窗口对象上的rootViewController,或者代理人持久tabBarControllerrootViewController );两者都是希望始终存在的顶级项目的示例。只需使用" self"否则最终会以部分视图呈现。

在已呈现的视图中是否有可靠的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];

});

1 个答案:

答案 0 :(得分: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];
}