How to dismiss the UIPopoverController?

13,042

Solution 1

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

Solution 2

Apple docs:

The popover controller does not call this method in response to programmatic calls to the dismissPopoverAnimated: method. If you dismiss the popover programmatically, you should perform any cleanup actions immediately after calling the dismissPopoverAnimated: method.

So the didDimiss delegate's method won't be called by itself.

Try:

[self.popover dismissPopoverAnimated:YES];
[self.popover.delegate popoverControllerDidDismissPopover:self.PopUp];
Share:
13,042
Nazik
Author by

Nazik

Coding is my Passion Coding is my Splendor Coding is my Gixxer ....................................................................................................................................

Updated on June 05, 2022

Comments

  • Nazik
    Nazik almost 2 years

    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?

  • Midhun MP
    Midhun MP over 11 years
    it's not the case here, he is initializing new instance for dismissing the old popover
  • Nazik
    Nazik over 11 years
    [controller setFs:self], it gives an error, How to pass the old instance when displaying the popover
  • Midhun MP
    Midhun MP over 11 years
    @NAZIK: still have issues ?
  • Nazik
    Nazik over 11 years
    @ Midhun MP, yes,error - popovercontroller not found on object of type 'PopOverViewController *'- what to do now?
  • Midhun MP
    Midhun MP over 11 years
    @NAZIK: please check your code once again, I think you made a mistake. Check whether you are passing [... setFS:self] and check the typr of fs, it should be Firstviewcontroller
  • Midhun MP
    Midhun MP over 11 years
    declare the fs in your SecondViewController class's @interface and create a property @property (nonatomic, assign) Firstviewcontroller *fs; if you are using non-arc and synthesize it
  • Midhun MP
    Midhun MP over 11 years
    @NAZIK: my mistake, please check above comment. Type of fs is Firstviewcontroller
  • Nazik
    Nazik over 11 years
    U said " Check whether you are passing [... setFS:self] and check the typr of fs" where to write this and what is 'setFS'
  • Nazik
    Nazik over 11 years
    When writing [controller setFS:self]; it gives an error- No visible @interface for 'PopoverViewController' declares the selector 'setFS:'
  • Midhun MP
    Midhun MP over 11 years
    your controller is of type SecondViewController is it ?
  • Midhun MP
    Midhun MP over 11 years
    I told you to put @property (nonatomic, assign) Firstviewcontroller *fs; this code on your SecondViewController's @interface. Did you do that ?
  • Nazik
    Nazik over 11 years
    yes I did it, and [fs.popoverController dismissPopoverAnimated:TRUE]; throws no error but it didnot dissmiss the popover
  • Nazik
    Nazik over 11 years
    what is [controller setFs:self]
  • Midhun MP
    Midhun MP over 11 years
    controller is the instance of your SecondViewController and you are setting the firstviewcontroller instance to fs using the setter method
  • Midhun MP
    Midhun MP over 11 years
    @NAZIK: Ok, please share that
  • Nazik
    Nazik over 10 years
    Hi Midhun, take a look at my question, pl answer if you can.
  • Midhun MP
    Midhun MP over 10 years
    @NAZIK: I added a comment in your question, please check. It'll work. I think you already got that answer
  • Nazik
    Nazik over 10 years
    Thanks for info, I got the solution.