Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How should I subclass NSWindow initialization in Objective-C

i want to create a subclass of NSWindow. this subclass needs to initialize some member variables before they are used. what is the best way to capture initialization in objective c? what i find is that init rarely gets called in a way that allows me to do this. NSWindow has a couple of initialization vectors that i would need to override. do i need to override each of them?

like image 267
shaheen Avatar asked Nov 24 '10 20:11

shaheen


1 Answers

Each class should have one so-called designated initializer. This is the init method that all other init methods call. That's the one to override. The documentation usually tells you which one the designated initializer is. In the case of NSWindow, it is:

initWithContentRect:styleMask:backing:defer:

This method is the designated initializer for the NSWindow class.

In addition to the designated initializer, you should also override -initWithCoder: if the class you subclass implements the NSCoding protocol. -initWithCoder: is the initializer that is used when an instance is instantiated from an archive (such as a NIB file).

See The Designated Initializer in Apple's "The Objective-C Programming Language".

like image 172
Ole Begemann Avatar answered Sep 27 '22 20:09

Ole Begemann