Search code examples
objective-ciosuinavigationcontrolleruiimagepickercontrolleruipopovercontroller

How to allow the user to select a UIImagePickerController's source type?


First let me tell you what I'm trying to do:

  1. When the user clicks on a button, I want to present a UIPopoverController with a UINavigationController as its contentViewController (this is on an iPad)
  2. The first UIViewController pushed into UINavigationController will be a custom view controller that has 2 buttons: "take picture" and "select from library".
  3. Touching the "take picture" button will setSourceType:UIImagePickerControllerSourceTypeCamera on the UIImagePickerController and push it into the UINavigationController
  4. Touching the "select from library" button will setSourceType:UIImagePickerControllerSourceTypePhotoLibrary on the UIImagePickerController and push it into the UINavigationController

I want to be able to do all of this and be able to allow a user to navigate backwards as the UINavigationController allows.

The problem is that since UIImagePickerController is also a UINavigationController, I cannot push it into another UINavigationController as stated in #3 and #4.

Questions:

  1. Is there a way for me to use UIImagePickerController with a custom view controller pushed before the camera / photo library view controllers?
  2. Or conversely, is there a way for me to get access to the camera / photo library view controllers to push them into a different UINavigationController?
  3. Or is there another way that I am completely missing here?

Solution

  • Adding the following code (as detailed here) was what finally solved this question for me.

    Use the navigationController:willShowViewController:animated: method to access the navigationBar.

    then with this code you can add a "cancel" 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 
    
    }