iOS如何用按钮关闭popover

时间:2012-02-13 20:51:01

标签: ios popover

在iOS上如何关闭popover按钮进入popover.I调用viewController进入popover但我无法关闭popover不解雇。我怎么能这样做请帮助

3 个答案:

答案 0 :(得分:0)

请使用:

- (void)dismissPopOver
{
    [self.popOver dismissPopoverAnimated:YES];
    self.popOver = nil;
}

按下按钮时调用该方法。

答案 1 :(得分:0)

我通常最终必须制作用于改变弹出窗口可见性的切换按钮。这就是我总是把它们连接起来的方式。我按钮的操作如下:

- (void)popoverButtonTapped:(id)button
{
    if (self.popover != nil)
    {
        // If the popover is open, close it
        [self.popover dismissPopoverAnimated:YES];
        self.popover = nil;
    }
    else
    {
        // Otherwise, open a new popover
        self.popover = ...;
        // This is important because you want to know if the popover was closed
        // by tapping outside it or some external event
        self.popover.delegate = self;
    } 
}

- (void)popoverControllerDidDismissPopover:(UIPopoverController *)popoverController
{
    if (popoverController == self.popover)
    {
        // nil out our reference since the popover was dismissed
        self.popover = nil;
    }
}

答案 2 :(得分:0)

您可以使用导航控制器&顶部有一个条形按钮。在导航控制器中插入第二个视图控制器。添加一个条形按钮项,并将其操作引用到一个方法,该方法将在第一个视图控制器中关闭弹出控制器。可能是您可以使用以下代码:

在创建UIPopoverController时,我假设您已经在第一个视图控制器的.h文件中添加了UIPopoverConterollerDelegate

在第一个视图控制器的.h文件中声明UIPopoverController *popover;

在创建弹出控制器的位置编写以下代码。

-(IBAction)createPopoverController
{
    SecondViewController *secondViewController = [[SecondViewController alloc] initWithNibName:@"SecondViewController" bundle:nil];

    UIBarButtonItem *dismissButton = [[UIBarButtonItem alloc] initWithTitle:@"Dismiss" style:UIBarButtonItemStyleBordered target:self action:@selector(dismissButtonPressed:)];

    [secondViewController.navigationItem setRightBarButtonItem:dismissButton];

    UINavigationController* navC = [[UINavigationController alloc] initWithRootViewController:secondViewController];
    popover = [[UIPopoverController alloc] initWithContentViewController:navC];
    popover.delegate = self;
    [popover setPopoverContentSize:CGSizeMake(secondViewController.view.frame.size.width, secondViewController.view.frame.size.height) animated:YES];
    [popover presentPopoverFromRect:yourButton.frame inView:self.view permittedArrowDirections:UIPopoverArrowDirectionUp animated:YES];
    [secondViewController release];
}

-(void)dismissButtonPressed:(id)sender
{
    [popover dismissPopoverAnimated:YES];
}

我希望它会对你有所帮助。 :)