Reputation: 1
I want to open a window programmatically because I need to send a variable to the window as it is opening. MacOS.
I have created a new window controller and a new view controller. Both are called from ViewController.
@IBAction func ButtonClicked(_ sender: Any) {
let SB = NSStoryboard (name: "Main", bundle: nil)
let NVC = SB.instantiateController(withIdentifier: "NewViewController") as! NewViewController
let NWC = SB.instantiateController(withIdentifier: "NewWindowController") as! NSWindowController
NVC.inputString = outputString
NWC.showWindow(self)
}
The new window never opens. NewViewController loads but no data is passed.
override func viewDidLoad() { // NewViewController
super.viewDidLoad()
print(inputString)
print("end")
}
Rather than open a window programmatically, I used the IB to generate a segue which then passed the data. The new controller was opened as "show". I assigned a name to the segue in the IB. Of course, that required overriding the prepare function.
override func prepare(for segue: NSStoryboardSegue, sender: Any?) {
if (segue.identifier == "TestSegue") {
let NVC:NewViewController = segue.destinationController as! NewViewController
NVC.name = "test"
}
}
The passed data was then accessed in NewViewController's viewDidLoad.
The code is, admittedly, simple, but it seems to work.
Upvotes: -1
Views: 125
Reputation: 1
Two view controllers: ViewController.swift and NewViewController.swift.
Define a segue in ViewController.swift
override func prepare(for segue: NSStoryboardSegue, sender: Any?) {
if (segue.identifier == "TestSegue") {
let NVC:NewViewController = segue.destinationController as! NewViewController
NVC.name = "test"
}
}
In NewViewController.swift, define a variable to receive a value passed from ViewController.swift, and access that variable in NewViewController.swift's viewDidLoad function.
Upvotes: -1