swift - 检测 UIViewController 何时被 tvOS 中的 Remote 菜单按钮关闭

标签 swift uiviewcontroller segue tvos dismiss

我正在开发一个 Apple TV 应用程序 (tvOS),其中 first view controller 通过 打开一个 second view controller继续。当我在第二个 View Controller 上选择一个选项时,它会在第一个 View Controller 上执行一个展开操作。

我的问题是当我按下远程菜单按钮时,second view controller modal dismiss automatically 我发现无法在第一个 View Controller 上执行操作或者被通知。

如何检测通过 segue 打开的 Controller 何时被 Remote 的菜单按钮关闭?

┌─────────────┐               ┌─────────────┐   
│ First View  │    ┌─────┐    │ Modal View  ├──┐
│ Controller  ├────┤segue├────▶ Controller  │  │
└─────────────┘    └─────┘    └─────────────┘  │
                ┌────────────┐      ┌───────┐  │
                │ Modal Auto │      │ Menu  │  │
  Action ??  ◀──┤  Dismiss   ◀──────│Button │◀─┘
                └────────────┘      └───────┘   

最佳答案

我相信这就是你想要完成的

// Put this in your FirstViewController
@IBAction func returnToFirstViewController(segue:UIStoryboardSegue) {
  print("This is called after  modal is dismissed by menu button on Siri Remote")
}

// Put this in your SecondViewController
override func viewDidLoad() {
  super.viewDidLoad()  

  // Do any additional setup after loading the view.

  let tapRecognizer = UITapGestureRecognizer(target: self, action: Selector("handleMenuPress:"))
  tapRecognizer.allowedPressTypes = [UIPressType.Menu.rawValue]
  view.addGestureRecognizer(tapRecognizer)
}

func handleMenuPress(recognizer: UITapGestureRecognizer) {
  self.performSegueWithIdentifier("YourUnwindSegueIdentifier", sender: nil)
}

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
  if segue.identifier == "YourUnwindSegueIdentifier" {
     // do any cleanup activities here if you need
  }
}

现在您必须建立一些 Storyboard连接。进入你的 SecondViewController 并从你的 Controller 图标按住 ctrl 拖动到你的退出图标,你会看到一个像这样的下拉菜单:

enter image description here

选择连接它的方法,然后您将在 Storyboard的 SecondViewController 中看到一个 Unwind Segue。为该 segue 提供标识符名称“YourUnwindSegueIdentifier”(这样我的示例代码就可以工作了——或者使用您想要的任何名称)。构建并运行,这应该可以满足您的需求。

关于swift - 检测 UIViewController 何时被 tvOS 中的 Remote 菜单按钮关闭,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34136953/

相关文章:

ios - 为什么 tableView reloadData 不调用 cellForRowAtIndexPath

ios - 使用固定的 UIView 页眉和页脚构建父 UIViewController。调整在页眉和页脚中继承 UIViewControllers 内容?

ios - 当应用程序返回前台时控制应用程序快照

ios - 向左滑动 View Controller 到动态 View

ios - 通过没有 Storyboard的函数从另一个类执行 Segue

ios - 从 Obj-c 到 swift 的 Segue 找不到属性

swift - 删除 PencilKit 中的内容

ios - swift3 中的 Button.titleLabel 错误

ios - 后台获取 NSPersistentCloudKitContainer 变化设置通知

swift - 无法在 Swift 3 中创建范围