UIPopoverController not dismissed when opened from self.navigationItem (inside UINavigationController)

前端 未结 4 1654
醉话见心
醉话见心 2021-01-24 21:06

I have a problem dismissing a popover that was launched from the navigationItem of a UINavigationController. It seems that the navigation item which is inserted by the UINavigat

4条回答
  •  时光说笑
    2021-01-24 21:44

    I've faced this problem recently and none of the solutions that I've seen around worked for me. Then after some research I've found out a way that works like a charm.

    First you need to add the UIStoryboardPopoverSegue to your class.

    @property (nonatomic, strong) UIStoryboardPopoverSegue *popoverSegue;
    

    Synthesize it inside of the class implementation:

    @synthesize popoverSegue;
    

    Afterwards, inside of the function called by your button when pressed add the following code:

    ([[popoverSegue popoverController] isPopoverVisible]) ? 
                [self.popoverSegue.popoverController dismissPopoverAnimated: YES] :
                [self performSegueWithIdentifier: @"popSegue" sender:nil];
    

    Now you are almost ready. inside of the method - (void) prepareForSegue:(UIStoryboard)segue sender:(id)sender add the following code:

    if([[segue identifier] isEqualToString:@"popSegue"]){
        self.popoverSegue = (UIStoryboardPopoverSegue*) segue;
        if(viewPopoverController == nil){
            viewController = [[UIViewController alloc] init];
            viewPopoverController = [[UIPopoverController alloc] initWithContentViewController:viewController];
        }
    }
    

    Now every time you press the button it will either show or dismiss your window, the window will also be dismissed if you press outside of the popover. I hope it can help someone.

提交回复
热议问题