我创建了一个UIPopoverController,并在单击UIButton时将其添加到视图控制器中,如下所示

- (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];
    }
}
btnShowPopoverUIButton中的viewcontrollerpopoverControllerUIPopoverController

单击按钮时,popovercontroller看起来很好。

但是当我单击UIButton中的secondviewcontroller时,它不会消失

我为此使用了以下代码
-(IBAction)y:(id)sender{
    fs =  [[Firstviewcontroller alloc] initWithNibName:@"FIrstscreen" bundle:[NSBundle mainBundle]];
    [fs.popoverController dismissPopoverAnimated:TRUE];
}

但这没有用。

当单击添加到popovercontroller的viewcontoller中的按钮时,如何关闭popovercontroller

最佳答案

您正在分配Firstviewcontroller的新实例,因此它不会消除前一实例的弹出窗口。

显示弹出框时,您需要传递旧实例,例如:

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

并像这样解散:
-(IBAction)y:(id)sender
{
    [fs.popoverController dismissPopoverAnimated:TRUE];
}

关于iphone - 如何关闭UIPopoverController?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/14317252/

10-11 07:06