UIBarButtonItem + popover segue creates multiple popovers

妖精的绣舞 提交于 2019-12-05 03:26:24

By the time you get messaged in -prepareForSegue:sender:, it's too late to cancel a segue.

In order to do this efficiently, you should create segues to your popovers from the view controller itself instead of the bar buttons so that they can still be programmatically executed. Now wire the UIBarButtonItems up to some methods that will conditionally present or dismiss the popover.

- (IBAction)showPopoverA
{
    if (self.popoverA.popoverController.popoverVisible)
        [self.popoverA.popoverController dismissPopoverAnimated:YES];

    [self performSegueWithIdentifier:@"ShowPopoverA"];
}

This is the proper way to do what you need to do:

- (BOOL)shouldPerformSegueWithIdentifier:(NSString *)identifier sender:(id)sender {
    if ([identifier isEqualToString:@"SurveyListPopover"]) {
        if (self.surveyListPopover == nil) {
            return YES;
        }
        return NO;
    }
    return YES;
}

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    if ([segue.identifier isEqualToString:@"SurveyListPopover"]) {
        // Assign popover instance so we can dismiss it later
        self.surveyListPopover = [(UIStoryboardPopoverSegue *)segue popoverController];
    }
}

This ensures that the segue will be cancelled if an instance of the popover has already been displayed. You just need to make sure your popover object has an identifier in the storyboard.

Combination of both made it for me

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([segue.identifier isEqualToString:@"showPopover"]) {
        self.tableOfContentsPopoverController = [(UIStoryboardPopoverSegue*)segue popoverController];
    }
}

- (BOOL)shouldPerformSegueWithIdentifier:(NSString *)identifier sender:(id)sender {
    if ([identifier isEqualToString:@"showPopover"]) {
        if (!self.tableOfContentsPopoverController.popoverVisible) {
            return YES;
        }
        return NO;
    }
    return YES;
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!