popoverControllerDidDismissPopover method is not c

2019-05-27 08:31发布

In my application I have a presentViewController, and inside it I have a button that opens a popover. In this popover I have a barButtonItem to save de data of this popover. I would like that when the user taps outside of the popover, the data could be saved too. I've tried to use the popoverControllerDidDismissPopover method in the presentViewController view. I have the delegate but when I tap outside of the popover this method is not called.

What can I do?

Thanks!!

5条回答
别忘想泡老子
2楼-- · 2019-05-27 08:41

I had the same issue. You will need to retain the popover object, that way the delegate method gets called. Its strange but it works.

@property (nonatomic, retain) UIPopoverController *popupObject;

UIPopoverController *popup = [[UIPopoverController alloc] initWithContentViewController:viewController];
popup.delegate = self;
[popup presentPopoverFromRect:presentationRect inView:self permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
self.popupObject = popup; //Retained


-(void) popoverControllerDidDismissPopover:(UIPopoverController *)popoverController
{
  //Do whatever operation you need to perform
  self.popupObject = nil; 
}
查看更多
叛逆
3楼-- · 2019-05-27 08:45

Add this line of code while adding popOver:

 popover.delegate = self;

Also register popOverDelegate in .h file where u present your popOver COntroller

@interface yourViewController : UIViewController<UIPopoverControllerDelegate>
查看更多
欢心
4楼-- · 2019-05-27 08:48

You probably already solved it, but I just faced the same problem. I'm holding a instance of UIPopoverController in my Viewcontroller and had it this way:

self.popover.delegate = self;
self.popover = [[UIPopoverController alloc] initWithContentViewController:wgtvc];

of course this doesn't work because I'm initializing the UIPopoverController AFTER setting the delegate, which overrides the delegate setting. So the correct way is to FIRST initialize the UIPopovercontroller and THEN setting the delegate

self.popover = [[UIPopoverController alloc] initWithContentViewController:wgtvc];
self.popover.delegate = self;

Maybe you are reinitializing your UIPopoverController somewhere - just set the delegate again after reinitializing.

查看更多
啃猪蹄的小仙女
5楼-- · 2019-05-27 08:48

I had the same problem and I solved it by handling it different for iOS8.

Presentation code

    UIViewController *searchViewController = [[UIViewController alloc] init];

    [[searchViewController view] addSubview:_searchOptions];
    [searchViewController setModalPresentationStyle:UIModalPresentationPopover];
    [searchViewController setPreferredContentSize:CGSizeMake(500, 400)];

    [_searchOptions setHidden:NO];
    [_searchOptions setFrame:[[searchViewController view] bounds]];
    [_searchOptions setAutoresizingMask:UIViewAutoresizingFlexibleWidthAndHeight];

    if (CRIdiomToolsIsIOS8OrHigher())
    {
        UIPopoverPresentationController *popOverPresentationController = [searchViewController popoverPresentationController];

        [popOverPresentationController setDelegate:self];
        [popOverPresentationController setSourceView:[_searchOptionsButton disclosureView]];
        [popOverPresentationController setSourceRect:[[_searchOptionsButton disclosureView] bounds]];

        [self presentViewController:searchViewController animated:YES completion:nil];
    }
    else
    {
        UIPopoverController *popOverControler = [[UIPopoverController alloc] initWithContentViewController:searchViewController];

        [popOverControler setDelegate:self];
        [popOverControler setPopoverContentSize:CGSizeMake(500, 400)];

        [popOverControler presentPopoverFromRect:[[_searchOptionsButton disclosureView] bounds] inView:[_searchOptionsButton disclosureView] permittedArrowDirections:UIPopoverArrowDirectionUp|UIPopoverArrowDirectionLeft animated:YES];
    }

Delegate calls

#pragma mark Delegate Methods: UIPopoverControllerDelegate

- (void)popoverControllerDidDismissPopover:(UIPopoverController *)popoverController
{
    [self showSearchOptions:NO animated:YES];
}

#pragma mark Delegate Methods: UIPopoverPresentationControllerDelegate

- (void)popoverPresentationControllerDidDismissPopover:(UIPopoverPresentationController *)popoverPresentationController
{
    [self showSearchOptions:NO animated:YES];
}
查看更多
啃猪蹄的小仙女
6楼-- · 2019-05-27 09:01

I know this questions is old but hopefully it helps someone out there...

The issue is in the initialization of your popover controller. If you have established the popover segue in the storyboard you need to have a reference to this popover in order for the delegate to be called when it is dismissed.

In your prepare for segue method:

Instead of:

    self.popoverController = [[UIPopoverController alloc]initWithContentViewController:segue.destinationViewController];
    self.popoverController.delegate = self;

You need:

   self.popoverController = [(UIStoryboardPopoverSegue *)segue popoverController];
    self.popoverController.delegate = self;

Then make sure to correctly handle if the when the popover should appear in

  • (BOOL)shouldPerformSegueWithIdentifier:(NSString *)identifier sender:(id)sender
查看更多
登录 后发表回答