2

I'm absolute newbie in Swift and OSX development, and I'm sorry if my question will be too noob. I want to understand principles of navigation between NSViewControllers.

I have my default ViewController, where are login and password fields and button to login. After click on button, returns token. And now I trying to change "view" to SecondViewController and save somewhere token, I will need it in future. How can I do it? and it possible to do this in function?:

@IBAction func loginButton(_ sender: Any) {
   .... 
}

Thank you!

Danila Kulakov
  • 1,082
  • 1
  • 12
  • 20

1 Answers1

2

You need to use segues to perform this action.

First make the segues connections between the ViewControllers using to storyboard editor. After that you need to give the segues an identifier on the storyboard's attributes inspector. Then in your code you can call the new ViewController by the segue like the code below.

With this code you can pass the data using the button:

class ViewController: NSViewController {

    var dataToPass: String = "DataToPass"

    override func viewDidLoad() {
        super.viewDidLoad()

    }

    @IBAction func loginButton(_ sender: Any) {

        performSegue(withIdentifier: NSStoryboardSegue.Identifier(rawValue: "segueIdentifier"), sender: self)
    }

    override func prepare(for segue: NSStoryboardSegue, sender: Any?) {

        if segue.identifier!.rawValue == "segueIdentifier" {
            let destinationViewController = segue.destinationController as! ViewController2

            destinationViewController.dataToReceive = dataToPass
        }
    }
}


class ViewController2: NSViewController {

    var dataToReceive: String

    override func viewDidLoad() {
        super.viewDidLoad()

    }
}

And with this code you will use the override viewWillAppear

class ViewController: NSViewController {

    var dataToPass: String = "DataToPass"

    override func viewDidLoad() {
        super.viewDidLoad()

    }

    override func viewWillAppear() {

        performSegue(withIdentifier: NSStoryboardSegue.Identifier(rawValue: "segueIdentifier"), sender: self)
    }

    override func prepare(for segue: NSStoryboardSegue, sender: Any?) {

        if segue.identifier!.rawValue == "segueIdentifier" {
            let destinationViewController = segue.destinationController as! ViewController2

            destinationViewController.dataToReceive = dataToPass
        }
    }
}


class ViewController2: NSViewController {

    var dataToReceive: String

    override func viewDidLoad() {
        super.viewDidLoad()

    }
}

In both cases you need to assure the data you want to pass to the other view controller is not null.

GuiDupas
  • 1,681
  • 2
  • 22
  • 44
  • Thank you for help. Now I can transfer data when I click button. But when I'm trying make seque between two `ViewControllers ` I have only (show/modal/sheet/popover/custom) options. Is some possibility to show ViewController2 in initial window? Replace ViewController to ViewController2. – Danila Kulakov Feb 07 '18 at 17:41
  • You can change the inicial view controller on your storyboard, but this way you will not be able to pass the data to it as it will be the first view controller. Another way you can do is to perform the segue on `viewDidLoad` or `viewWillAppear` keeping the `override prepare` passing the data. – GuiDupas Feb 07 '18 at 17:44
  • I didn't understand how to do it. Can I ask you for a code sample? Thank you one more time! – Danila Kulakov Feb 07 '18 at 18:23
  • I have edited the answer to include the example. Take a look. – GuiDupas Feb 07 '18 at 18:34
  • Will check it tomorrow morning, now I’m away from computer. But thanks anyway! – Danila Kulakov Feb 07 '18 at 20:53
  • When I run programm open 2 windows... It doesn't replace ViewControllers. – Danila Kulakov Feb 08 '18 at 09:25
  • But the `segue` in Swift for MacOS creates a new window. It's the normal behavior. – GuiDupas Feb 08 '18 at 11:04
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/164740/discussion-between-guidupas-and-danila-kulakov). – GuiDupas Feb 08 '18 at 11:04