加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

swift – 在tvOS中通过遥控器的菜单按钮解除UIViewController被

发布时间:2020-12-14 04:59:43 所属栏目:百科 来源:网络整理
导读:我正在开发Apple TV应用程序(tvOS),其中第一个视图控制器通过segue打开第二个视图控制器.当我在第二个视图控制器上选择一个选项时,它会在第一个视图控制器上执行一个展开操作. 我的问题是当我按下远程菜单按钮时,第二个视图控制器模式自动解除,我发现无法在
我正在开发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图标,你会看到一个下拉列表:

enter image description here

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读