2

Possible Duplicate:
UIPopovercontroller dealloc reached while popover is still visible

Im creating a universal app and trying to get an image selected from Camera roll. Works fine on iPhone, iPad wants a pop over, so have done that, now keep getting an error

-[UIPopoverController dealloc] reached while popover is still visible.

Ive researched:

Stack Link

Stack Link

and google, nothing has solved this issue

Stuck now, any advice appreciated

Ive implemented the popover delegate in .h

.m

 - (void)logoButtonPressed:(id)sender /////////////iPad requires seperate method ////////////////
 {                                        

if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) {

LogCmd();
UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
imagePicker.delegate = self;
imagePicker.allowsEditing = YES;
////////
imagePicker.modalPresentationStyle = UIModalPresentationCurrentContext;
////////
[self presentModalViewController:imagePicker animated:YES];


}

else

{

    // We are using an iPad
    UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];
    imagePickerController.delegate = self;
    UIPopoverController *popoverController=[[UIPopoverController alloc] initWithContentViewController:imagePickerController];
    popoverController.delegate=self;


    [popoverController presentPopoverFromRect:((UIButton *)sender).bounds inView:sender permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];


   }


  }

I have also now tried this:

.h

 @property (strong) UIPopoverController *pop;

.m

if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) {            ///code added

LogCmd();
UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
imagePicker.delegate = self;
imagePicker.allowsEditing = YES;
[self presentModalViewController:imagePicker animated:YES];


    ///code added////////////////////////////

}

else {

    if (self.pop) {
        [self.pop dismissPopoverAnimated:YES];
    }
    // If usingiPad
    UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];

    UIPopoverController *popoverController=[[UIPopoverController alloc] initWithContentViewController:imagePickerController];


    [popoverController presentPopoverFromRect:((UIButton *)sender).bounds inView:sender permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
Community
  • 1
  • 1
JSA986
  • 5,870
  • 9
  • 45
  • 91
  • Try the second link Carl, I have already looked at that which is why I put in in my question to indicate already researched – JSA986 Oct 01 '12 at 22:30
  • 1
    Yes, but it's the correct answer. Although to be fair you must read the comments. I may just edit the answer in fact. You are allocating the popover and then assigning it to a local variable, that variable goes out of scope and the popover is deallocated. The point of having the property is to assign the popover to it. So don't do `UIPopoverController *popoverController = [[UIPopoverController alloc] ...` but rather `self.pop = [[UIPopoverController alloc] ...`. – Carl Veazey Oct 01 '12 at 22:39
  • I added to the answer there, and I have to agree it was a bit confusing and didn't match up with the code, so I apologize. – Carl Veazey Oct 01 '12 at 22:45
  • Thanks Carl, ive changed my code to as per your guidance and after 4 hours its finally working! thank you sooooooooo much!, can you make this an answer so I can accept it and bring the bromance? – JSA986 Oct 01 '12 at 22:49

1 Answers1

8

You instantiate the popover and assign it to a local variable here:

UIPopoverController *popoverController=[[UIPopoverController alloc] initWithContentViewController:imagePickerController];

As soon as the method returns, the variable goes out of scope and the object is deallocated since it has no owner anymore.

What you should do is declare a strong property to assign the popover to. You have already done so with your pop property. So now all you need to do is at the time you allocate the popover, assign it to your property. This makes you the owner of the object so it won't get deallocated.

self.pop = [[UIPopoverController alloc] initWithContentViewController:imagePickerController];
[self.pop presentPopoverFromRect:((UIButton *)sender).bounds inView:sender permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];

Hope this helps!

Carl Veazey
  • 18,392
  • 8
  • 66
  • 81