Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Showing UIImagePickerController in UIPopoverController with existing UINavigationController (adding back button)

So I have a UIPopoverController what houses my UINavigationController where I have my UITableViewController however one of my options on the UITableView is to go and select an image with the UIImagePickerController... Now on the iPhone I can simply use presentModalViewController:animated: however calling that from within a UIPopoverController causes a crash so thats not possible...

I also know the UIImagePickerController needs its own UINavigationController so I can't just push pushViewController:animated: either...

So I figured out that if I keep a link to the UIPopoverController I created, I can then use setContentViewController:animated: to switch to the UIImagePickerController's viewController...

However, I am now stuck at giving the user a way to go back to the previous UINavigationController as I need to be able to add a cancel button to the UIImagePickerController but when I try to do this the cancel button won't get added...

Heres my code that i'm using

-(void)doPhotoalbums {
    if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) {

        UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
        [imagePicker setDelegate:self];
        [imagePicker setSourceType:UIImagePickerControllerSourceTypePhotoLibrary];
        [imagePicker setContentSizeForViewInPopover:CGSizeMake(320, 480)];

        UIBarButtonItem *cancel = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:nil];
        [imagePicker.navigationItem setLeftBarButtonItem:cancel];

        //[self presentModalViewController:imagePicker animated:YES];
        [[self parentPopoverController] setContentViewController:imagePicker animated:YES];

    } else {
        [UIAlertView showMessage:@"This device does not have any photo albums."];
    }
}

So my question is.. Does anybody know how I can get around this? either by adding a cancel/back button what I can hook up to make the navigationControllers switch back or another way to present this (i'd like to avoid switching between two UIPopoverControllers but I don't know what else I can do..

Thanks

Liam

like image 498
liamnichols Avatar asked May 05 '12 14:05

liamnichols


1 Answers

Ahh.. after a little break I found this: https://discussions.apple.com/thread/1710435?start=0&tstart=0

using the UINavigationControllerDelegate you can use the navigationController:willShowViewController:animated: method to access the navigationBar.. then with some code (below) you can add a button.

if ([navigationController isKindOfClass:[UIImagePickerController class]]) {

    UINavigationBar *bar = navigationController.navigationBar;
    UINavigationItem *top = bar.topItem;

    UIBarButtonItem *cancel = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(imagePickerControllerDidCancel:)];
    [top setLeftBarButtonItem:cancel];

} else { 

    //do non imagePickerController things 

}
like image 82
liamnichols Avatar answered Sep 17 '22 15:09

liamnichols