0

I'm working on a project and my ViewController file is starting to get very long. Hence, I want to define all my functions in a second ViewController so I can delete some code from my first ViewController. This works, except for when I try to refer to a value from a textField defined in my FirstViewController, it returns nil. I am new to MacOS development so I would greatly appreciate simple/specific feedback.

This is an example of my first ViewController (it initializes variables and uses functions):

class FirstViewController: NSViewController, NSTextFieldDelegate {

    @IBOutlet weak var firstName: NSTextField!
    let lastName = "Smith"  

    @IBAction func buttonPressed(_ sender: Any) {
        SecondViewController().printUserName()  
    }
}

This is my second ViewController (it defines functions):

class SecondViewController: NSViewController, NSTextFieldDelegate {

    func printUserName() {
       print(FirstViewController().firstName!) // this returns nil :(
       print(FirstViewController().lastName) // this returns "Smith" :)
    }
}
Zara
  • 1
  • 1
  • 3
    You need to learn about classes and instances. You are making new empty view controllers, not referring to the existing ones. – matt Jul 03 '21 at 22:34
  • Thank you for your response. How can I refer to the First View Controller without using segues? – Zara Jul 03 '21 at 22:39
  • Some other options are closures, protocols, or notifications. You can do research on the pros and cons of each one. – Mochi Jul 03 '21 at 22:47
  • Thanks I will look into these. Which is the simplest option? – Zara Jul 03 '21 at 22:49
  • 2
    Without addressing what would be a "good" refactoring, if the goal is simply to declutter the view controller file, you could create an `extension` of `FirstViewController` in a separate file, and put supporting methods there, keeping outlets and actions directly in the class definition. The extension will automatically have access to the class's properties and methods. – Chip Jarred Jul 03 '21 at 23:05
  • Try to understand answer on this link : https://stackoverflow.com/questions/26676687/passing-values-from-one-view-controller-to-another-in-swift – Hardik Thakkar Jul 07 '21 at 10:33

1 Answers1

0

If you use the UINavigationController to move to the second VC, you can simply define a second view and then access the variables in that view.

example, FirstViewController

class FirstViewController: NSViewController, NSTextFieldDelegate {

    @IBOutlet weak var firstName: NSTextField!
    let lastName = "Smith"  

    @IBAction func buttonPressed(_ sender: Any) {
        guard let secondView = self.storyboard?.instantiateViewController(identifier: "SecondViewController") as? SecondViewController else {
                return
            }
    self.navigationController?.pushViewController(secondView, animated: false)
    secondView.textValue = firstName.text 
    }
}

SecondViewController

class SecondViewController: NSViewController, NSTextFieldDelegate {

    var textValue: String?

    override func viewDidLoad() {
       printUserName()
    }


    func printUserName() {
       if let text = textValue {
            print(text)
       }
  
    }
}
Hailey
  • 302
  • 1
  • 7