我正在开发Apple TV应用程序(tvOS),其中第一个视图控制器通过segue打开第二个视图控制器.当我在第二个视图控制器上选择一个选项时,它会在第一个视图控制器上执行一个展开操作.
我的问题是当我按下远程菜单按钮时,第二个视图控制器模式自动解除,我发现无法在第一个视图控制器上执行操作或被通知.
如何检测通过segue打开的控制器何时被遥控器的菜单按钮解除?
┌─────────────┐ ┌─────────────┐ │ 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 } }
现在你必须建立一些故事板连接.进入你的SecondViewController并按住Ctrl键从你的Controller图标拖动到你的Exit图标,你会看到一个下拉列表:
选择连接它的方法,然后您将在故事板中的SecondViewController中看到Unwind Segue.给该segue标识符名称为“YourUnwindSegueIdentifier”(因此我的示例代码将起作用 – 或使用您想要的任何名称).构建并运行,这应该可以满足您的需求.
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。