简体   繁体   中英

One Way Segue in tvOS

I have a Swift3 tvOS app, in which I transition from one ViewController to another using performSegue(withIdentifier: ...). In the second ViewController I use the Menu button for UI interaction using pressesBegan with press.type == UIPressType.menu.

This works fine if the second ViewController is entered directly (ie is the default ViewController), but with the transition, the app shows the first ViewController again, when pressing the menu button (menu doubles as back in tvOS). My second ViewController gets the key too, but the UI results are never shown, as the second ViewController disappears. I have tried with different segue types, but to no avail.

Questions:

1) Is there a way to "consume" the Menu key event, so it never gets to the OS?

2) Alternatively, can I somehow make a "one way segue"?

3) Alternative 2: Can I programmatically unset the relationship between the two ViewControllers? What is it that makes one "parent" to the other? I do not use a navigationcontroller or anything, so it must be the segue.

Thanks,

If you want to implement this part of the application without the UINavigationController , you can just set the root UIViewController programmatically. First of all, add some storyboard id to the ViewController2 in the Main.storyboard . Then, replace this part of code:

performSegue(withIdentifier: "toViewController2", sender: self)

with

let appDelegate = UIApplication.shared.delegate as! AppDelegate
let mainStoryBoard = UIStoryboard(name: "Main", bundle: nil)
let secondViewController = mainStoryBoard.instantiateViewController(withIdentifier: "ViewController2") as! ViewController2
appDelegate.window?.rootViewController = secondViewController

After the first UIViewController will appear you will see the second UIViewController and menu button's handler will work as you wish (no back segue).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM