Clicking the Picture Button Twice on iPad


#1

While working through chapter 13, I found a bug using the iPad simulator. I was able to correct it, but I’m not 100% certain that my fix is proper.

Clicking the picture button to reveal the popover works fine. Clicking the picture button a second time while the popover is displayed generates a sigabrt with this message:

My fix for this was to wrap the offending bit of code in an if statement that checks the visibility of the popover:

if (![imagePickerPopover isPopoverVisible]) {
 
    if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad) {
        [imagePickerPopover release];
     
        imagePickerPopover = [[UIPopoverController alloc]
                              initWithContentViewController:imagePicker];
        [imagePickerPopover presentPopoverFromBarButtonItem:sender permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
    } else {
        [self presentModalViewController:imagePicker animated:YES];
    }
     
}

This leads me to two questions.

  1. Did I just create a great big memory leak?

I’m very new to the platform, and I realize memory management is an issue. Since imagePicker is created and released within the scope of the method, I think I’m safe, but creating imagePicker in the first place seems inefficient if it will ultimately never be used.

  1. Would UI conventions dictate that tapping the picture button a second time should dismiss the popover rather than ignoring the tap (in which case my fix should be amended to dismiss the popover)?

Thanks for your help! Fan-freaking-tastic book so far.


#2

I think this is actually a bug in my code that I didn’t catch in the 2nd printing. Fortunately, in the 2nd edition, the popover stuff is much nicer.

Anyway, for this particular issue, yeah, this will work. (Although, be careful about the modalViewController stuff inside your encompassing if statement, it should work out okay, but the if statement should really be moved to inside the if block that checks if you are on the iPad.)

Also, use, the convention is that the popover would dismiss. Thus, you might change it to something like this:

if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad) {
   if ([imagePickerPopover isPopoverVisible]) {
        [imagePickerPopover dismissPopoverAnimated:YES]; 
        // make sure to autorelease here, otherwise, it will be destroyed before the animation starts
        // and you'll get an exception
        [imagePickerPopover autorelease];
        imagePickerPopover = nil;
   } else {
        [imagePickerPopover release];
        imagePickerPopover = [[UIPopoverController alloc]
                              initWithContentViewController:imagePicker];
        [imagePickerPopover presentPopoverFromBarButtonItem:sender permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
   }
} else {
    [self presentModalViewController:imagePicker animated:YES];
}

#3

Thanks for the response!