简体   繁体   中英

Dismissing UIPopoverController when using ABNewPersonViewController

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;
    }

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM