i have added a UIPopover to my navigationbar when the user touches the rightbarbuttonitem.
I want to know whats the cleanest coding for showing and dismissing the popover. With the code below, it works, but not correct.
If the user touches the rightbarbuttonitem first, the popover appears. Now he can choose a cell,.. works. Or you can touch outside the popover and it disappears. Both ways correctly.
But when touching the rightbarbuttonitem and after that, the same touch on that icon again, you have to touch it twice to bring up that popover again. (Not on the first touch).
How should that correctfunction look like? Thanks for your time..
-(void) downloads:(UIBarButtonItem*)button{
NSLog(@"downloads");
if(tableViewController == nil) {
tableViewController = [[[TableViewController alloc] initWithStyle:UITableViewStylePlain] autorelease];
self.popoverController = [[[UIPopoverController alloc] initWithContentViewController:tableViewController]autorelease] ;
[self.popoverController presentPopoverFromBarButtonItem:barButtonItem permittedArrowDirections:UIPopoverArrowDirectionUp animated:YES];
tableViewController.delegate = self;
}else {
[self.popoverController dismissPopoverAnimated:NO];
tableViewController.delegate = nil;
tableViewController = nil;
//[self.popoverController release];
//self.popoverController = nil;
}
}
//delegate
-(void) selectedTable:(NSString*)text{
NSLog(@"selectedTable: %@", text);
[self.popoverController dismissPopoverAnimated:YES];
}
Comments
Your
downloads:method checks fortableViewControllervalue and make decision if UIPopoverController should be displayed or dismissed. But yoursselectedTable:method does dismisspopoverControllerand does not settableViewControllerto nil thus it doesn’t work as expected by you.And I also don’t see
UIPopoverControllerDelegateimplementation, especiallypopoverControllerDidDismissPopover:which will tell you whenUIPopoverControllerwas dismissed (by touching outside yourUIPopoverControllerfor example).Memory Management
Read memory management guide.
[self.popoverController release]withself.popoverController = nilleads to crash – one release and second release during setting to nil. (I assume yourpopoverControllerproperty retains).