views:

48

answers:

1

I have an "add Contact" button which when on iPhone I present a navigation controller with root view controller of an ABNewPersonController modally.

If on iPad I have got a popover which I can display with the new person controller inside - nice.

The problem comes when trying to dismiss.

I can dismiss the popover when touching done or cancel within my implementation of didCompleteWithNewPerson using;

if(self.popoverController != nil)
    [popoverController dismissPopoverAnimated:YES];  

However, this doesn't dismiss when touching outside the popover.

I've returned YES for my popoverControllerShouldDismissPopover method and set the delegate of my popover to this. I've put an NSLOG inside this method and it's not dropping in there - Am I missing something?

Does anyone know how to dismiss the popover when touching outside?

Update - More Code

-(IBAction)contactsClicked:(id) sender{

    ABNewPersonViewController *newPersonView = [[ABNewPersonViewController alloc] init];
    [newPersonView setNewPersonViewDelegate:self];
    [newPersonView setDisplayedPerson:newPerson];

        UINavigationController *addContactNavController = [[UINavigationController alloc] initWithRootViewController:newPersonView];
        [newPersonView release];


        if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {

            if(self.popoverController == nil){
                UIPopoverController *popover = [[UIPopoverController alloc] initWithContentViewController:addContactNavController];

                self.popoverController = popover;
                self.popoverController.delegate = self;
                [popover release];
            }
            CGRect frame = [sender frame];
            [popoverController presentPopoverFromRect:frame inView:self.view permittedArrowDirections:UIPopoverArrowDirectionRight animated:YES];
        } else {
            [self presentModalViewController:addContactNavController animated:YES];
            [addContactNavController release];
        }
    }
-(void)unknownPersonViewController:(ABUnknownPersonViewController *)unknownPersonView didResolveToPerson:(ABRecordRef)person{
    [self dismissModalViewControllerAnimated:YES];
}
-(void)newPersonViewController:(ABNewPersonViewController *)newPersonViewController didCompleteWithNewPerson:(ABRecordRef)person {
    NSLog(@"DONE OR CANCEL clicked!!!!"); //prints
    if (self.popoverController != nil) {
        [popoverController dismissPopoverAnimated:YES];
    }
    [self dismissModalViewControllerAnimated:YES];
}

The Done and Cancel buttons of the new person controller work, dismissing the controller and the popover (when running on iPad). I guess this means the delegate for the ABNewPersonViewController is implemented correctly. (?)

I'm guessing that I may be confusing the issue by having multiple controllers and my popover delegate method is getting hidden or something?

Thanks in advance

EDIT - Delegate method

-(BOOL)popoverControllerShouldDismissPopover:(UIPopoverController *)thePopoverController{
    NSLog(@"clicked outside the popover");//never prints
    return YES;
    }
A: 

From the docs:

Taps inside the popover window do not automatically cause the popover to be dismissed. Your view and view controller code must handle actions and events inside the popover explicitly and call the dismissPopoverAnimated: method as needed.

You should use the popover delegate methods –popoverControllerShouldDismissPopover: to listen for when it's about to be dismissed and do your saving etc. there.

Also, you should use self not this.

nevan
I have implemented -popoverControllerShouldDismissPopover, as mentioned in OP, but control never seems to get into here. I meant self (c# background)
mizzle
Did you set your object up as the `UIPopoverControllerDelegate`?
nevan
@nevan yes. It is a touch outside the popover that I want to cause the dismiss.
mizzle
You'll need to post more code. It looks like you haven't set up the delegate properly.
nevan
posted almost all the code + my own speculation
mizzle
There are no delegate methods in that code. Have you implemented `–popoverControllerShouldDismissPopover:` and conformed to `UIPopoverControllerDelegate`?
nevan
I have conformed and added dimiss method above
mizzle