以模态方式呈现UIImagePickerController时出错

时间:2013-01-28 14:40:05

标签: 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我提出了一个新的UIViewSGFoldingAttachmentView类的成员,其中包含此代码:

- (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 个答案:

答案 0 :(得分:4)

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

self.window.rootViewController = navController;