Moisie Moisie - 7 months ago 40
Swift Question

Accessing variables or functions in a 'parent' class

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.

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

I've already looked at other posts that seem to cover the same thing - Access variable in different class - Swift , How to create a global variable? , Access variable in different class - Swift and Pass variables from one ViewController to another in Swift - but none of them seem to do exactly what I want.

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.

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.

// 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.
}

}


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?

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.

Any suggestions very welcome - thanks.

Answer

In an OSX storyboard NSViewController has a property presentingViewController which represents the parent view controller.

In SecondViewController you can refer to the presenting controller with

let parentViewController = presentingViewController as! ViewController