如何使用单个按钮和单个操作方法显示/隐藏UIPopoverController

时间:2012-01-12 16:02:22

标签: objective-c ios uipopovercontroller

以下方法与导航栏按钮相关联。按下按钮时,会显示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];


}

我想要做的是在第二次按下按钮时隐藏弹出窗口:

  1. 点击 - show popover
  2. 点击 - 隐藏popover
  3. 我该怎么做?

1 个答案:

答案 0 :(得分:6)

这应该做你想要的:

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