以模态方式呈现 UIImagePickerController 时出错

Posted

技术标签:

【中文标题】以模态方式呈现 UIImagePickerController 时出错【英文标题】:Error with presenting UIImagePickerController modally 【发布时间】:2013-01-28 14:40:04 【问题描述】:

在我的 ios 6 应用程序中以模态方式呈现 UIImagePickerController 时遇到一个奇怪的问题。 XCode 给了我这个错误:

Warning: Attempt to present <UIImagePickerController: 0x1e025040> on <UINavigationController: 0x1d51ce00> whose view is not in the window hierarchy!

我的AppDelegate 中有以下窗口层次结构:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions

    UIWindow *window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    self.window = window;

    firstViewController = [[SGNewMailViewController alloc] init];
    navController = [[UINavigationController alloc] initWithRootViewController:firstViewController];
    self.window.rootViewController = firstViewController;
    [self.window makeKeyAndVisible];
    return YES;

在我的SGNewMailViewController 中,我提出了一个新的UIView,它是SGFoldingAttachmentView 类的成员,其中包含以下代码:

- (void)choosePhotoPressed 
    SGNewMailViewController *mailVC = [((SGAppDelegate *)[UIApplication sharedApplication].delegate).firstViewController.navigationController.viewControllers lastObject];
    [mailVC displayCameraRoll];

最后,这是我的SGNewMailViewController displayCameraRoll 方法:

- (void)displayCameraRoll 
    if ([UIImagePickerController isSourceTypeAvailable:
         UIImagePickerControllerSourceTypeSavedPhotosAlbum])
    
        UIImagePickerController *imagePicker =
        [[UIImagePickerController alloc] init];
        imagePicker.delegate = self;
        imagePicker.sourceType =
        UIImagePickerControllerSourceTypePhotoLibrary;
        imagePicker.mediaTypes = [NSArray arrayWithObjects:
                                  (NSString *) kUTTypeImage,
                                  nil];
        imagePicker.allowsEditing = NO;
        [self presentViewController:imagePicker animated:YES completion:nil];
    

那么可能是什么问题?它是否在另一个类提供的自定义视图中?我应该如何解决它?提前致谢!

【问题讨论】:

【参考方案1】:

使 UINavigationController 成为窗口的根视图控制器。这应该可以解决问题

self.window.rootViewController = navController;

【讨论】:

以上是关于以模态方式呈现 UIImagePickerController 时出错的主要内容,如果未能解决你的问题,请参考以下文章

以模态方式呈现 UIImagePickerController 时出错

如何以模态方式呈现标准 UIViewController

总是以模态的方式呈现

以模态方式呈现的 TableViewController 可抵抗滚动且难以消除

如何在不使用 presentViewController 的情况下以模态方式呈现自定义视图?

将 MFMailComposeViewController 推送到导航堆栈上?未以模态方式呈现