In a test Swift project, I am subclassing NSWindowController
. My NSWindowController
subclass is designed to work with a particular Nib file. It is desirable, then, that when my window controller is initialized, the nib file is automatically loaded by the window controller instance. In Objective-C, this was achieved by doing:
@implementation MyWindowController
- (id)init {
self = [super initWithWindowNibName:"MyWindowNib"]
if (self) {
// whatever
}
return self
}
@end
Now, in Swift this is not possible: init()
cannot call super.init(windowNibName:)
, because the later is declared not as a designated initializer, but as a convenience one by NSWindowController
.
How can this be done in Swift? I don't see a strightforward way of doing it.
P.S.: I have seen other questions regarding this topic, but, as long as I've been able to understand, the solutions all point to initialize the Window Controller by calling init(windowNibName:)
. Please note that this is not the desired beheaviour. The Window Controller should be initialized with init()
, and it should be the Window Controller itself who "picks up" its Nib file and loads it.
If you use the init()
just to call super.init(windowNibName:)
, you could instead just override the windowNibName
variable.
override var windowNibName: String {
get {
return "MyWindowNib"
}
}
Then there should be no need to mess with the initializers.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With