从self.navigationItem打开时UIPopoverController没有被解雇(在UINavigationController内部) [英] UIPopoverController not dismissed when opened from self.navigationItem (inside UINavigationController)
问题描述
我在解决从UINavigationController的navigationItem启动的popover时遇到问题。似乎UINavigationController插入的导航项不会触发UIPopoverController的解雇。通常,当你在弹出窗口外面敲击时,它会变暗。但是当您点击导航项时,弹出窗口不会被解除。更糟糕的是,如果你点击触发弹出窗口的按钮,你将获得弹出窗口的第二个实例。
I have a problem dismissing a popover that was launched from the navigationItem of a UINavigationController. It seems that the navigation item which is inserted by the UINavigationController does not trigger dismissal of the UIPopoverController. Usually, when you tap outside a popover, it is dimissed. But when you tap on the navigation item, the popover is not dismissed. Worse, if you tap the button which triggers the popover, you'll get a second instance of the popover.
所有这一切都是通过故事板完成的:
- 创建一个视图,将其嵌入到UINavigationView中,以便在顶部获得一个navigationItem。
- 将UIBarButtonItem放入navigationItem(左侧或右侧,与导航堆栈上的初始视图无关)。
- 定义另一个视图,并将一个segue从UIBarButtonItem拖到此视图中。
- 将segue设置为popover。
All of this is done using storyboards: - Create a view, embed it into a UINavigationView so it gets a navigationItem on the top. - Put a UIBarButtonItem into the navigationItem (left or right, doesn't matter for the initial view on the navigation stack). - Define another view, and drag a segue from the UIBarButtonItem to this view. - Set the segue to be popover.
弹出窗口打开后,我无法通过点击navigationItem来解除它。我无法相信这按设计工作,所以我怀疑我错过了什么。
Once the popover is open, I cannot dismiss it by tapping on the navigationItem. I cannot believe this "works as designed", so I suspect I missed out on somthing.
虽然我的目标是尽可能少地编程(这就是故事板的内容,不是吗?),我想到了解决方法:第一个解决方法来到我的想法是在navigationItem中添加一个UITapGestureRecognizer,它会在检测到navigationItem上的点击时关闭popover。不幸的是,navigationItem似乎不是UIVIew,所以它缺少addGestureRecognizer:方法......
Although my goal is to program as little as possible (that's what storyboards are about, aren't they?), I thought about workarounds: The first workaround that came to my mind was to add a UITapGestureRecognizer to the navigationItem, which would dismiss the popover when it detected a tap on the navigationItem. Unfortunately, the navigationItem seems to not be UIVIew, so it lacks the addGestureRecognizer: method...
编辑:向self.navigationController.navigationBar添加UITapGesturerecognizer是可能的,但是防止任何点击到达navigationBar上的UIBarButtonItems。是的,我本可以预料到的。
Adding a UITapGesturerecognizer to self.navigationController.navigationBar is possible, but prevents any tap to reach the UIBarButtonItems on the navigationBar. Yes, I could have expected that.
非常感谢你的帮助,
nobi
Thanks a lot for help, nobi
推荐答案
这是故事板中弹出窗口的完整描述。假设您的控制器出现在popover中,名为 MyPopupController
,请执行以下操作:
Here's the complete description for popovers in storyboards. Assuming your controller to appear in the popover is named MyPopupController
, do this:
- 使用样式
Popover
定义从主场景到MyPopupController
场景的segue。 -
将属性添加到
MyPopupController.h
- Define a segue from the main scene to the
MyPopupController
scene, using stylePopover
. Add a property to
MyPopupController.h
@property (weak, nonatomic) UIPopoverController *popover;
在 MyPopupController.m $ c $中合成属性c>
@synthesize popover = _popover
在主场景控制器的 prepareForSegue:sender:
方法中,设置 popover
property:
In the prepareForSegue:sender:
method of the main scene controller, set up the popover
property:
UIStoryboardPopoverSegue *ps = (UIStoryboardPopoverSegue *)segue;
MyPopupController *dc = ps.destinationViewController;
dc.popover = ps.popoverController;
在 viewWillAppear:
方法中 MyPopupController
,添加以下代码(不要忘记 [super viewWillAppear]
):
In the viewWillAppear:
method of MyPopupController
, add the following code (don't forget [super viewWillAppear]
as well):
self.popover.passThroughViews = nil;
您现在应该完成。
感谢Robert的基本想法 - 我只需要找到正确的位置,因为 presentPopoverFromBarButtonItem
不会被使用使用故事板。请注意,将(5)放入 viewDidLoad
不起作用。
Thanks to Robert for the basic idea - I just had to find the correct place because presentPopoverFromBarButtonItem
is not used when using storyboards. Note that putting (5) into viewDidLoad
does not work.
玩得开心!
这篇关于从self.navigationItem打开时UIPopoverController没有被解雇(在UINavigationController内部)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!