2

in watchOS I used presentControllerWithName to show a View Controller and to pass the context in this way

presentControllerWithName("NameOfTheViewController", context:"PassedContext")

Which is the equivalent in tvOS?

Best Regards

Gualty
  • 251
  • 1
  • 4
  • 13

2 Answers2

3

As noted in other answers, the way to programmatically show another view controller in tvOS (or iOS) is performSegueWithIdentifier:sender:. (Or presentViewController:animated:completion: if you're not getting your VCs from a storyboard flow.)

But you might not need to do it programmatically. In watchOS it's sometimes easiest to do it that way, but in iOS & tvOS, it's common to make controls directly perform storyboard transitions entirely from Interface Builder. Just control-drag (right-click-drag) from the button to another view controller. (More step-by-step instructions in Xcode Help.)

enter image description here

Unlike watchOS, the view controller transitions in iOS & tvOS don't include a way to pass context information. Not as part of the API, at least — you have to include a bit of glue code yourself to do that. How to do that is a pretty common question.

If you're using storyboard segues (generally, you should), the prepareForSegue:sender: method is typically where you do this — you get a reference to the new view controller that's about to be shown, and use some function or property you've defined on that view controller to pass it some context. It often looks something like this:

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    if segue.identifier == mySegueIdentifier {
        guard let destination = segue.destinationViewController as? MyViewControllerClass
            else { abort("unexpected storyboard segue") }
        destination.someProperty = someValue
    }
}

You can find good examples of this when you create a new Xcode project with the Master-Detail App template.

Community
  • 1
  • 1
rickster
  • 124,678
  • 26
  • 272
  • 326
  • Thank you for both answer. How can I pass an argument to the VC? In watchOS I passed the context but a variable will be the same – Gualty Dec 01 '15 at 21:44
2

tvOS is more similar to iOS than it is to watchOS, although they all have some similarities. In tvOS (like in iOS) you can use both performSegueWithIdentifier:sender: or presentViewController:animated:completion: depending on your situation.

For more on this, you can check out the UIViewController class reference.

Jeff Lewis
  • 2,846
  • 1
  • 19
  • 24