ios - 关闭 iOS 9 上由 segue 创建的 Popover

标签 ios uipopover uipresentationcontroller

Stack Overflow 上有很多关于此问题的问题和解答,但它们仅适用于 iOS 8 及之前的版本。

iOS 9 弃用了很多东西,SO 上的答案不再有效。

也就是说,我通过执行这样的转场来呈现弹出窗口

[self performSegueWithIdentifier:@"myPopover" sender:self];

这个segue是在当前viewController和popover使用的viewController之间创建的。没有涉及任何按钮。弹出窗口锚定到 View 。

问题在于 prepareForSegue:identifier

[segue destinationViewController]

是一个UIViewController并且

[[segue destinationViewController] popoverPresentationController]

是新的UIPopoverPresentationController并且该对象不再提供dismiss api。

相反,我们应该使用

[self dismissViewControllerAnimated:YES completion:nil];

关闭弹出窗口,但这对我没有任何影响。

我的情况是这样的:我有一个带有文本字段的弹出窗口。如果用户隐藏键盘,我想关闭弹出窗口。

所以我这样做了:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];

然后

- (void)keyboardWillHide:(NSNotification *)notification {
  [self dismissViewControllerAnimated:YES completion:nil];
}

但这根本没有效果。

我还尝试在弹出 View Controller 内创建一个展开转场,并从呈现的 View Controller 调用它,但这会使应用程序崩溃。

最佳答案

刚刚尝试了一下,一切似乎都很完美。

  • 您的 View Controller 层次结构是什么(导航 Controller 等)?
  • 您是否在呈现弹出窗口的同一 View Controller 上正确调用了 dismissViewControllerAnimated:completion:
  • 确保 subview Controller 和导航 Controller 之间没有混淆。
  • 您还可以记录弹出 View Controller 的 presentingViewController 进行检查。

关于ios - 关闭 iOS 9 上由 segue 创建的 Popover,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34176715/

相关文章:

ios - 获取 iPod 播放器项目的 iTunes ID

ios - 将提示设置为文本字段中文本的开头

ios - 显示键盘时,Popover 内的 UIScrollView 内的 UITextView 不完全可见

ios - 如何使用 swift 使标签在 subview 中居中

swift - 作为弹出窗口呈现的 UIViewController 可以是它自己的 popoverPresentationController 委托(delegate)吗?

ios - 解除模态呈现的 View Controller 时框架不正确

ios - 错误的 UITableViewAutomaticDimension 高度,仅适用于 iPhone 5

ios - 我如何使用 PhoneGap 在 Windows 操作系统中创建 iOS 应用程序?

ios - 可以从其他 ViewController 类更改 UILabel 的字体

ios - 选择时关闭 UIPopover