iphone - 以模态方式呈现 UIImagePickerController 时出错

标签 iphone ios objective-c cocoa-touch uiview

我在我的 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];
    }
}

那么可能是什么问题呢?它是在另一个类中呈现的自定义 View 中吗?我该如何解决?提前致谢!

最佳答案

使 UINavigationController 成为窗口的 Root View Controller 。这应该可以解决问题

self.window.rootViewController = navController;

关于iphone - 以模态方式呈现 UIImagePickerController 时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14564413/

相关文章:

iphone - Telnet Iphone 开发

iphone - UITextView - 禁用垂直滚动

ios - 如何确保使用了我的自定义 URL 协议(protocol)?

ios - 如何获取 SWIFT 3 中特定控件的标识符名称(通过 Storyboard创建)以从数据库设置值

iOS Facebook API 使用 GRAPH API 从时间线获取数据

objective-c - Mac OSX : Determing whether user account is an Active Directory user vs. 本地用户使用objective-c

iphone - iPhone 文本编辑器上的语法突出显示

iphone - 如何在 iPhone 中使用 URL 链接播放音乐文件?

iphone NSDate - 获取今天的日期并强制时间

iOS 自定义 UITextField 与导出初始化