I have a NSViewController
that has a class variable as follows:
class ServerAdminViewController: NSViewController, NSTextFieldDelegate, MyTableViewDelegate {
var myClass = MyClass()
func showAdmin(window: NSWindow, myClass: MyClass) {
self.myClass = myClass
window.makeKeyAndOrderFront(self)
NSApp.activate(ignoringOtherApps: true)
}
An instance of this class is instantiated in the App initialization. Later when user clicks on a menu item, I want to show a window and use that value as a singleton for the app, during the time this window is open, also being able to modify myClass
. However, the line where the instance variable is declared is being executed multiple times and after the self.myClass = myClass
assignment, resetting the object (this is a menubar app that opens a window).
Tried
Several things, ex. removing the parameterless constructor from myClass
, declaring that variable private and with a "!", adding init methods to the ViewController
(without success) and some other failed attempts.
Question
How to make myClass
a singleton and share it accross the application (i.e. make it available to this window) without reinitializing it?