Error with presenting UIImagePickerController moda

2019-05-14 06:39发布

问题:

I have a strange problem presenting the UIImagePickerController modally in my iOS 6 app. The XCode gives me this error:

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

I have the following window hierarchy in my 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;
}

In my SGNewMailViewController I present a new UIView, a member of SGFoldingAttachmentView class which has this code in it:

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

In the end, here's my SGNewMailViewController displayCameraRoll method:

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

So what can possibly be the problem? Is it in the custom view presented from another class? How should I fix it? Thanks in advance!

回答1:

Make the UINavigationController to be the root view controller of your window. This should fix the problem

self.window.rootViewController = navController;