【问题标题】:presentViewController-- Attempt to present __ on __ whose view is not in the window hierarchypresentViewController-- 尝试在其视图不在窗口层次结构中的 __ 上呈现 __
【发布时间】:2014-10-06 20:14:16
【问题描述】:

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

基本思想是我尝试访问窗口对象上的rootViewController,或者应用程序委托的持久化tabBarController(即rootViewController);两者都是希望始终存在的顶级项目的示例。否则,仅使用“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];

});

【问题讨论】:

    标签: ios presentviewcontroller


    【解决方案1】:

    因为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];
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-06-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-09-17
      • 2023-03-31
      • 2016-03-26
      • 2018-06-06
      相关资源
      最近更新 更多