0

I'm attempting to do a presentViewController with an UIImagePickerControl in order to display a standard Camera Roll photo picker. This works end-to-end in most of my app. The place where it does not work is when I want to use the imagePicker inside an already presented viewController; the view to present the album is unable to be presented.

The basic idea is I'm trying to either access the rootViewController on the window object, or the app delegate's persisted tabBarController (which is the rootViewController); both as examples of top level items that are hopefully always present. Just using "self" otherwise ends up as a partial view presenting it.

Is there a reliable way to presentViewController inside an already presentedView?

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];

});
4

1 回答 1

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];
}
于 2014-10-06T20:34:45.190 回答