通过tvOS中的遥控器菜单按钮检测UIViewController何时被解除

Ati*_*ika 6 uiviewcontroller dismiss segue swift tvos

我正在开发Apple TV应用程序(tvOS),其中一个视图控制器通过segue打开第二个视图控制器.当我在第二个视图控制器上选择一个选项时,它会在第一个视图控制器上执行一个展开操作.

我的问题是当我按下远程菜单按钮时,第二个视图控制器模式自动解除,我发现无法在第一个视图控制器上执行操作或被通知.

如何检测通过segue打开的控制器何时被遥控器的菜单按钮解除?

???????????????               ???????????????   
? First View  ?    ???????    ? Modal View  ????
? Controller  ??????segue?????? Controller  ?  ?
???????????????    ???????    ???????????????  ?
                ??????????????      ?????????  ?
                ? Modal Auto ?      ? Menu  ?  ?
  Action ??  ????  Dismiss   ????????Button ????
                ??????????????      ?????????   
Run Code Online (Sandbox Code Playgroud)

ear*_*rip 1

我相信这就是你想要实现的目标

// 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
  }
}
Run Code Online (Sandbox Code Playgroud)

现在您必须建立一些故事板连接。进入 SecondViewController 并按住 Ctrl 键从 Controller 图标拖动到 Exit 图标,您将看到如下所示的下拉菜单:

在此输入图像描述

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