How to show/hide UIPopoverController using a singl

2019-07-07 00:22发布

The following method is associated with a nav bar button. When the button is pressed, it shows a UIPopoverController

- (IBAction) showTablePopUp:(id) sender {

if (self.tablesPopoverController == nil) {

    TablesPopOverViewController *tables = [[TablesPopOverViewController alloc]initWithNibName:@"TablesPopOverViewController" bundle:[NSBundle mainBundle]]; 

    UIPopoverController *popover = [[UIPopoverController alloc] initWithContentViewController:tables]; 

    popover.delegate = self;
    popover.popoverContentSize=CGSizeMake(280.0, 327.0);
    [tables release];

    self.tablesPopoverController = popover;
    [popover release];
}

[self.tablesPopoverController presentPopoverFromBarButtonItem:sender permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];


}

What I want to do is hiding the popover when the button is pressed the second time:

  1. Click - show popover
  2. Click - hide popover

How can I do it?

1条回答
Viruses.
2楼-- · 2019-07-07 00:48

This should do what you want:

- (IBAction) showTablePopUp:(id) sender {
    if (self.tablesPopoverController == nil) {

        TablesPopOverViewController *tables = [[TablesPopOverViewController alloc]initWithNibName:@"TablesPopOverViewController" bundle:[NSBundle mainBundle]]; 

        UIPopoverController *popover = [[UIPopoverController alloc] initWithContentViewController:tables]; 

        popover.delegate = self;
        popover.popoverContentSize=CGSizeMake(280.0, 327.0);
        [tables release];

        self.tablesPopoverController = popover;

        [self.tablesPopoverController presentPopoverFromBarButtonItem:sender 
            permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];

    } else {
        [self.tablesPopoverController dismissPopoverAnimated: YES];
        self.tablesPopeverController = nil;
    }
}
查看更多
登录 后发表回答