This is my first app since slogging through the Learning Objective C 2.0 and Beginning iPhone 3 books, so please bear with me. The response I got on another forum (http://www.iphonedevsdk.com/forum/iphone-sdk-development/52850-cant-seem-stop-duplicate-popovers.html) wasn't helpful at all. Even if he pointed me to the relevant section of his "definitive guide" that helped explain where I failed and why, that would have helped.
I've got this one user experience bug I just can't seem to shake. You tap a toolbar button and a popover displays. That's good. Tap the toolbar button again and I get a 2nd popover right on top of the first. Not good.
FlickrPhotoPickerController *flickrPhotoPickerView = [[FlickrPhotoPickerController alloc] init];
UIPopoverController *photoPopover = [[UIPopoverController alloc] initWithContentViewController:flickrPhotoPickerView];
[photoPopover setDelegate:self];
[flickrPhotoPickerView setPopover:photoPopover];
[flickrPhotoPickerView setMainImage:PPimageView];
if ([photoPopover isPopoverVisible]) {
NSLog(@"Yep, visible!");
[photoPopover dismissPopoverAnimated:YES];
} else {
NSLog(@"Nope, not visible!");
[photoPopover presentPopoverFromBarButtonItem:sender permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
}
[flickrPhotoPickerView release];
The thing is, the if keeps evaluating to false, even if the button is pressed a second time.
If I do this:
if ([photoPopover isPopoverVisible]) {
NSLog(@"Yep, visible!");
[photoPopover dismissPopoverAnimated:YES];
}
FlickrPhotoPickerController *flickrPhotoPickerView = [[FlickrPhotoPickerController alloc] init];
UIPopoverController *photoPopover = [[UIPopoverController alloc] initWithContentViewController:flickrPhotoPickerView];
[photoPopover setDelegate:self];
[flickrPhotoPickerView setPopover:photoPopover];
[flickrPhotoPickerView setMainImage:PPimageView];
NSLog(@"Nope, not visible!");
[photoPopover presentPopoverFromBarButtonItem:sender permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
[flickrPhotoPickerView release];I get "'photoPopover' undeclared (first use in this function)"
My sense is it's a scope issue for the popover controller, but I'm not sure how to handle the init properly for my needs. (basically, I understand that I may be failing to understand a basic fundamental concept)
Now, his response was:
Use a property for the popover. Then dismiss the old popover before you create a new one.
I considered that, but I wasn't getting anywhere there either. Using @property and @synthesize, and changing my code to:
if ([photoPopover isPopoverVisible]) {
NSLog(@"Yep, visible!");
[photoPopover dismissPopoverAnimated:YES];
}
FlickrPhotoPickerController *flickrPhotoPickerView = [[FlickrPhotoPickerController alloc] init];
[photoPopover initWithContentViewController:flickrPhotoPickerView];
[photoPopover setDelegate:self];
[flickrPhotoPickerView setPopover:photoPopover];
[flickrPhotoPickerView setMainImage:PPimageView];
NSLog(@"Nope, not visible!");
[photoPopover presentPopoverFromBarButtonItem:sender permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
[flickrPhotoPickerView release];No popover would even appear. If I don't use @synthesize, wouldn't I still have to alloc right there where I was before? Wouldn't that leave me back at square one? Should I be overloading the setter?