iPad使用弹出视图内的按钮关闭弹出视图

3
我在我的弹出控制器中有一个按钮。我想用它来关闭弹出框,因此我试图访问主视图控制器(即“根”视图控制器)的一个方法(dismissPopover)。
注意:关闭弹出框的方法已经设置好并在根VC中工作,该VC是委托。如果我调用它,它会关闭弹出框。我只需要从弹出窗口访问该方法。
为此,我在AppDelegate中设置了一个属性,并像下面这样获取了rootVC的实例:self.rootController = (ViewController*)self.window.rootViewController;。然后,我将根VC类和AppDelegate导入到弹出视图控制器的类中,似乎给我访问根VC和方法的权限,但结果没有触发该方法。你知道我在这里缺少什么吗?
#import "ViewController.h"
#import "AppDelegate.h"

按钮连接的操作:

- (IBAction)dismissPopover:(id)sender {

//Checking the button works, it does:
NSLog(@"dismissPopover, from popover");

//Trying to get an instance of the rootViewController, the "presenting view controller"                                  
ViewController *rootVC = [(AppDelegate *)[[UIApplication sharedApplication] delegate] rootController];

//trying to access the method in the rootVC that dismisses the popover
[rootVC dismissPopover];

//Tried the following code, does nothing:
//[self dismissPopoverAnimated:YES];
}

注意:最终我放弃了使用弹出式窗口,因为它变得有点复杂。我尝试将我的视图控制器加载到一个UIView中(这样我就可以将nib文件的内容加载到一个弹出式视图中)。但这也变得有些复杂。所以,目前我只是在编程中构建我想要的界面,并将其放在一个UIView中。到目前为止,效果很棒。

5个回答

8
  1. dismissPopoverAnimated: is a method of UIPopoverController class. so, you need a popover controller reference in your 'root' view controller.

    MyRootViewController.myPopoverController = thePopover;
    
  2. the button is in your 'root' view controller, and in it's action method:

    [self.myPopoverController dismissPopoverAnimated:YES];
    

有趣。方法已经可以成功地为我想要访问的类工作了。只需要从弹出窗口中访问该方法,我就完成了,我想。但我会仔细考虑你的解决方案。 - OWolf

8
在iOS 8中,您可以使用dismissViewControllerAnimated:completion:从弹出窗口内部(如果它来自一个segue)关闭弹出窗口。但是在iOS 7(或更低版本)中无法使用该方法。

这是我直觉上做的事情,弹出窗口消失了。问题在于:当我以编程方式进行操作时,我的PopoverPresentationControllerDelegate没有被调用。但是如果我点击弹出窗外部使其自动消失,它确实被调用了。我还需要做什么来确保我的委托得到正确的通知? - Christian

3

当单击弹出框之外的区域时,Popover会自动关闭。如果您想要通过按钮来关闭它,您可以在dismissPopover方法内使用以下代码:

         [self.popoverController dismissPopoverAnimated:YES];

你不需要做这些繁琐的工作!


谢谢!但我知道这个。我不希望它在你点击外面时消失。我只想在按下按钮时它消失。顺便说一下,这是为了节省我在其他领域的一些繁琐工作。 - OWolf
我的意思是,在你的动作方法中使用这个方法来解除它,顺便说一句,我在类似的情况下也使用了它。 - Meseery
类型为 'X *' 的对象上找不到属性 'popoverController'。 - Ashish Awaghad

1

[self dismissViewControllerAnimated:YES completion:nil];

这是解决方案; 您只需要一个IBoutlet或者将目标添加到您的按钮,然后调用上面的代码行。


-1

我遇到了同样的问题

只需在您的buttonClickMethod中执行:

[yourPopoverController dismissPopoverAnimated:YES];

希望你能帮忙!
祝好!

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接