How should I write an init method for a class which is sub-classing an NSObject
- Let’s say i have a class named :
CustomButton subclassing UIButton - I have an empty nib file named
CustomButton.nib which has a single
UIButton in it. - IN interface builder the classname for the button is set to “CustomButton”
How would you write an init method for this class so that it loads from the nib file?
If you need custom initialization inside CustomButton, use this:
This will not be called if you instantiate the button directly in code, only when instantiated bu the nib loader. Instantiating through code means you need to use
initWithFrameso override that one. You may actually want to have initWithCoder and initWithFrame call the same initAlways method or something.Also, it’s important to know the Objective-C concept of “designated initializer” (look it up in objective C docs) because it can be a little confusing to people used to other OO languages.