Accessing variables or functions in a 'parent' class
<p>I'm quite new to programming in general - and to Swift in particular - but there's one thing I've been pounding my head on recently.</p>

<p>I'm trying to access a variable in a 'parent' class - but I can't work out how to get there.</p>

<p>I've already looked at other posts that seem to cover the same thing - <a href="https://stackoverflow.com/questions/24333142/access-variable-in-different-class-swift">Access variable in different class - Swift</a> , <a href="https://stackoverflow.com/questions/26195262/how-to-create-a-global-variable">How to create a global variable?</a> , <a href="https://stackoverflow.com/questions/24333142/access-variable-in-different-class-swift">Access variable in different class - Swift</a> and <a href="https://stackoverflow.com/questions/24044108/pass-variables-from-one-viewcontroller-to-another-in-swift">Pass variables from one ViewController to another in Swift</a> - but none of them seem to do exactly what I want.</p>

<p>I'm wanting to be able to have a window of Preference settings, which I can set and have apply to variables in my 'main' class - but I can't see how to pass the variables from the Preferences window up to the 'main' class.</p>

<p>Here's an example in which I've defined an NSTextField on my first ViewController, which I want to be able to modify in my second ViewController and have it update in the first. My actual application will probably need many such instances, in order to provide a full set of modifiable preferences.</p>

<pre><code>//  ViewController.swift

import Cocoa


class ViewController: NSViewController {


lazy var sheet2ViewController: NSViewController = {
    return self.storyboard!.instantiateControllerWithIdentifier("sheet2") as! NSViewController}()


@IBAction func openPanel(sender: AnyObject) {
    displaySheet()
}


@IBOutlet weak var textField: NSTextField!


var textString : String = "" {
    didSet {
        textField.stringValue = textString
    }
}


func displaySheet() {

    self.presentViewControllerAsSheet(sheet2ViewController)

}



override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view.
}

override var representedObject: AnyObject? {
    didSet {
    // Update the view, if already loaded.
    }
}


}



//  SecondViewController.swift

import Cocoa

class SecondViewController: NSViewController {

@IBOutlet weak var textField2: NSTextField!


@IBAction func closeButton(sender: AnyObject) {
    ???.textString = textField2.stringValue // How to address the ViewController variable here?
    self.dismissController(self)

}


override func viewDidLoad() {
    super.viewDidLoad()
    // Do view setup here.
}

}
</code></pre>

<p>As you see, the issue I have is in the closeButton function in the SecondViewController class - how to pass the value back up to the textString variable in the ViewController class?</p>

<p>I've been looking at delegation and NSNotificationCenter - but I really don't know if I'm barking up the right trees here - it feels like I'm going to have to implement something much more complicated than I would expect for such a (seemingly) simple requirement.</p>

<p>Any suggestions very welcome - thanks.</p>