Search code examples
iosipaduipopovercontroller

UIPopoverController for iPad "must not be called with `nil`" error


I am trying to make an iPhone app work in an iPad but the UIPopoverController is comming back with error.

- (IBAction)photoTapped1 {
if(UI_USER_INTERFACE_IDIOM()== UIUserInterfaceIdiomPhone){
// If in editing state, then display an image picker; if not, create and push a photo view controller.
    if (self.editing) {
        UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
        imagePicker.delegate = self;
        [self presentViewController:imagePicker animated:YES completion:nil];
        [imagePicker release];
    } else {
        RecipePhotoViewController *recipePhotoViewController = [[RecipePhotoViewController alloc] init];
        recipePhotoViewController.hidesBottomBarWhenPushed = YES;
        recipePhotoViewController.recipe = recipe;
        [self.navigationController pushViewController:recipePhotoViewController animated:YES];
        [recipePhotoViewController release];
    }
}else{
    if (self.editing){
        self.popover = [[UIPopoverController alloc] initWithContentViewController:popover];
        self.popover.delegate =self;
        [popover release];
    }else{
         RecipePhotoViewController *recipePhotoViewController = [[RecipePhotoViewController alloc] init];
         recipePhotoViewController.hidesBottomBarWhenPushed = YES;
         recipePhotoViewController.recipe = recipe;
         [self.navigationController pushViewController:recipePhotoViewController animated:YES];
         [recipePhotoViewController release];
    }}}

The error I am getting is: 'NSInvalidArgumentException', reason: '-[UIPopoverController initWithContentViewController:] must not be called with nil.'

Anyone available to give me a hand on this code, I have looked on the internet for solutions and samples but can not seem to make it work.

Thank you.

___ added to original question_____

I am adding the recipePhotoViewController here, I am assuming that ImageView manipulation is the same for iPhone and iPad.

my.h File

@class Recipe;

@interface RecipePhotoViewController : UIViewController {
@private
    Recipe *recipe;
    UIImageView *imageView;
}

@property(nonatomic, retain) Recipe *recipe;
@property(nonatomic, retain) UIImageView *imageView;

@end

Here is my .m file

@implementation RecipePhotoViewController

@synthesize recipe;
@synthesize imageView;

- (void)loadView {
self.title = @"Photo";

imageView = [[UIImageView alloc] initWithFrame:[UIScreen mainScreen].applicationFrame];
imageView.autoresizingMask = UIViewAutoresizingFlexibleHeight |      UIViewAutoresizingFlexibleWidth;
imageView.contentMode = UIViewContentModeScaleAspectFit;
imageView.backgroundColor = [UIColor blackColor];

self.view = imageView;   }
- (void)viewWillAppear:(BOOL)animated {
imageView.image = [recipe.image valueForKey:@"image"];}
- (BOOL)shouldAutorotateToInterfaceOrientation:  (UIInterfaceOrientation)interfaceOrientation  {
 return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}
- (void)dealloc {
[imageView release];
[recipe release];
[super dealloc];
}   @end

Solution

  • You are initializing the popover controller in a wrong way:

    self.popover = [[UIPopoverController alloc] initWithContentViewController:popover];
    

    You should pass the controller you would like to display inside of the popover -- not the popover itself (which is nil since you have not yet initialised it)!

    Maybe this would do it for you?

        UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
        imagePicker.delegate = self;
        self.popover = [[UIPopoverController alloc] initWithContentViewController:imagePicker];
    

    If this is correct, then you should also present the pop over you have just created: – presentPopoverFromRect:inView:permittedArrowDirections:animated:

    -- e.g.:

    [self.popover presentPopoverFromRect:sender.frame inView:[self view] permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
    

    where sender is the argument to :

    - (IBAction)photoTapped1:(id)sender {