Swift 4 pass data from NSViewController to NSTabViewController

196 Views Asked by At

I have a NSView controller called LOGIN

I have a NSTabViewController called LISTS and this has two tabs: playlistLists - associated with the class PlaylistLists, albums - associated with the class Albums

I need to pass a variable from login to playlistLists and i think something like this should work (its from another post), but this is for ios and I need it for macOS

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    let barViewControllers = segue.destination as! UITabBarController
    let destinationViewController = barViewControllers.viewControllers?[0] as! FirstViewController
    destinationViewController.test = "Hello TabBar 1"

    // access the second tab bar
    let secondDes = barViewControllers.viewControllers?[1] as! SecondViewController
    secondDes.test = "Hello TabBar 2" }

so I change it to this:

override func prepare(for segue: NSStoryboardSegue, sender: Any?) {
    let barViewControllers = segue.destinationControlles as! NSTabViewController
    let destinationViewController = barViewControllers.viewControllers?[0] as! playlistLists
    destinationViewController.test = "Hello TabBar 1"

    // access the second tab bar
    let secondDes = barViewControllers.viewControllers?[1] as! SecondViewController
    secondDes.test = "Hello TabBar 2"  }

This line I can't figure out how to change it for macOS:

    let destinationViewController = barViewControllers.viewControllers?[0] as! playlistListsts

or maybe is not the right way to do it.

Thanks for your help

1

There are 1 best solutions below

1
NRitH On

When you use as! to cast from one type to another, you have to specify a class (or struct) type, not a variable or property name. So if playlistsLists is a property of type PlaylistLists, then your line needs to be

let destinationViewController = barViewControllers.viewControllers?[0] as! PlaylistList