gpt4 book ai didi

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

转载 作者:搜寻专家 更新时间:2023-10-30 22:17:44 24 4
gpt4 key购买 nike

我正在开发一个 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com