Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to dismiss the UIPopoverController?

I have created a UIPopoverController and added it to a view controller when clicking an UIButton as follows

- (void)viewDidLoad
{
    [super viewDidLoad];
    controller = [[SecondViewController alloc] initWithNibName:@"SecondViewController" bundle:[NSBundle mainBundle]];
    popoverController = [[UIPopoverController alloc] initWithContentViewController:controller];
}

- (IBAction)showPopover:(UIButton *)sender
{
    if ([popoverController isPopoverVisible]) {
        [popoverController dismissPopoverAnimated:YES];
    } else {
               CGRect popRect = CGRectMake(self.btnShowPopover.frame.origin.x,
                                self.btnShowPopover.frame.origin.y,
                                self.btnShowPopover.frame.size.width,
                                self.btnShowPopover.frame.size.height);
          [popoverController presentPopoverFromRect:popRect
                                       inView:self.view
                     permittedArrowDirections:UIPopoverArrowDirectionAny
                                     animated:YES];
    }
}

btnShowPopover is the UIButton in the viewcontroller, popoverController is the UIPopoverController.

The popovercontroller appears fine while clicking the button.

But it won't get dismissed when I click the UIButton in the secondviewcontroller

I used the following code for that

-(IBAction)y:(id)sender{
    fs =  [[Firstviewcontroller alloc] initWithNibName:@"FIrstscreen" bundle:[NSBundle mainBundle]];
    [fs.popoverController dismissPopoverAnimated:TRUE];
}

But it didn't work.

How to dismiss the popovercontroller when clicking the button in a viewcontoller that was added to the popovercontroller?

like image 429
Nazik Avatar asked Jan 30 '26 18:01

Nazik


1 Answers

You are allocating a new instance of Firstviewcontroller, so it won't dismiss the previous instance's popover.

You need to pass the old instance when you are displaying the popover like:

- (void)viewDidLoad
{
    [super viewDidLoad];
    controller = [[SecondViewController alloc] initWithNibName:@"SecondViewController" bundle:[NSBundle mainBundle]];
    popoverController = [[UIPopoverController alloc] initWithContentViewController:controller];
    [controller setFs:self]
}

And dismiss like:

-(IBAction)y:(id)sender
{
    [fs.popoverController dismissPopoverAnimated:TRUE];
}
like image 53
Midhun MP Avatar answered Feb 02 '26 08:02

Midhun MP



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!