UIPopoverController not dismissed when opened from self.navigationItem (inside UINavigationController)(从 self.navigationItem(在 UINavigationController 内部)打开时,UIPopoverController 未解除)

本文介绍了从 self.navigationItem(在 UINavigationController 内部)打开时,UIPopoverController 未解除的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在关闭从 UINavigationController 的 navigationItem 启动的弹出框时遇到问题.似乎由 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 的点击时会关闭弹出框.不幸的是,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 是可能的,但会阻止任何点击到达导航栏上的 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.

非常感谢您的帮助,诺比

Thanks a lot for help, nobi

推荐答案

这是故事板中弹出框的完整描述.假设你的控制器出现在弹出框名为 MyPopupController,这样做:

Here's the complete description for popovers in storyboards. Assuming your controller to appear in the popover is named MyPopupController, do this:

  1. 定义从主场景到 MyPopupController 场景的转场,使用样式 Popover.
  2. MyPopupController.h 中添加一个属性

  1. Define a segue from the main scene to the MyPopupController scene, using style Popover.
  2. Add a property to MyPopupController.h

@property (weak, nonatomic) UIPopoverController *popover;

  • MyPopupController.m

    @synthesize popover = _popover
    

  • 在主场景控制器的prepareForSegue:sender:方法中,设置popover属性:

    UIStoryboardPopoverSegue *ps = (UIStoryboardPopoverSegue *)segue;
    MyPopupController *dc = ps.destinationViewController;
    dc.popover = ps.popoverController;
    

  • MyPopupControllerviewWillAppear:方法中,添加如下代码(别忘了[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(在 UINavigationController 内部)打开时,UIPopoverController 未解除的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!