0

So I am writing this iPad app that starts on a main screen and then from there you can go to a settings page. In the settings page you have the ability to select a picture from the photo album using a UIImagePickerController in a popover view.

If I go to the settings page and then press the back button to return to the main page everything works as expected. But if I go to settings and pick an image the back button on the page will not let me go back to the main page.

The popover and UIImagePickerController seem to be working fine so I do not know what is causing this. Here is my code for the UIImagePickerController.

- (IBAction)imagePick1:(id)sender {
    pickerController = [[UIImagePickerController alloc] init];
    [pickerController setDelegate:self];
    [pickerController setSourceType:UIImagePickerControllerSourceTypeSavedPhotosAlbum];
    [pickerController setAllowsEditing:NO];

    popoverController = [[UIPopoverController alloc] initWithContentViewController:pickerController];
    [popoverController setDelegate:self];
    [popoverController presentPopoverFromRect:[[self imageButton1] frame] inView:[self view] permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
}

...

- (void)imagePickerController:(UIImagePickerController *)pickerController1 didFinishPickingMediaWithInfo:(NSDictionary *)info

{
    image1 = [info objectForKey:UIImagePickerControllerOriginalImage];      
    _image1.image = image1;

    [popoverController dismissPopoverAnimated:YES];
}

When I try to press the back button the app freezes and it won't respond to any more commands. What am I doing wrong?

Edit: I ran it again and this is the error I got in the log when pressing the back button.

-[__NSCFType dismissPopoverAnimated:]: unrecognized selector sent to instance 0x7128cd0

Also here is the beginning of my header file.

@interface ViewController : UIViewController
<UIPickerViewDataSource, UIPickerViewDelegate, UIImagePickerControllerDelegate, UINavigationControllerDelegate, UIPopoverControllerDelegate>{
    UIPopoverController *popoverController;
}
@property (nonatomic, retain) UIPopoverController *popoverController; 
lfitzgibbons
  • 783
  • 1
  • 5
  • 18

1 Answers1

0

Possible duplicate. See here

Have you declared the delegate in the header file as well like below? Please also make sure your delegates conforms to the UINavigationControllerDelegate as well, as the UIImagePickerControllerDelegate directly inherits from UINavigationControllerDelegate, however all the UINavigationControllerDelegate methods are optional.

@interface MyViewController : UIViewController <UIImagePickerControllerDelegate, UINavigationControllerDelegate>

edit: Based on your edit, it appears that you need to also add the "UIPopoverControllerDelegate" to your header file. You also need to add [popoverController setDelegate:self] after your allocation.

edit 2: Ok now you need to declare this method as well in your implementation (.m) file.

and return yes.

- (BOOL)popoverControllerShouldDismissPopover:(UIPopoverController *)popoverController {
    return YES;
}

edit 3: Why are you are setting dismissPopoverAnimated to NO and releasing popoverController while it is still showing?

[popoverController dismissPopoverAnimated:NO];
[popoverController release];

try setting the dismissPopoverAnimated to YES and remove the release for now. By the way, is popoverController retained as a property in your header file? Are you using ARC? if you are using ARC you should not be calling release on the popoverController.

edit 4:

try modifying the header to this:

UIPopoverController *_popoverController;

@property (nonatomic, retain) UIPopoverController *popoverController

and add @synthesize popoverController = _popoverController; to your implementation file.

now start replacing all the instances of "popoverController" with "_popoverController" in your implementation file.

Also replace [popoverController dismissPopoverAnimated:YES]; with

if(_popoverController != nil)
    [_popoverController dismissPopoverAnimated:YES];

let me know the results below.

Community
  • 1
  • 1
kushyar
  • 1,191
  • 1
  • 6
  • 10
  • If this is a duplicate, please flag it accordingly instead of answering. Also, please refrain from extended discussions in comments. – BoltClock May 24 '13 at 08:05