I am looking to find out how to correctly subclass UIView
with a custom init method passing in parameters.
I have read the UIView
class reference documentation here: https://developer.apple.com/library/ios/documentation/uikit/reference/uiview_class/uiview/uiview.html
At present the UIView is being created in the UIViewController
and can be moved/refactored into its own subclass. Also at present the view is created completely in code and the frame is calculated by the constraints added to the view itself.
The documentation says the following
initWithFrame: - It is recommended that you implement this method. You can also implement custom initialization methods in addition to, or instead of, this method.
Question
As I do not create a frame as a starting point for the view / neither is it loaded from a XIB what is the correct process to subclass this?
Would this be correct:
-(id)init {
NSLog(@"%s",__PRETTY_FUNCTION__);
self = [super init];
if (self) {
// Init code
[self spmCustomInit];
}
return self;
}
-(void)spmCustomInit {
NSLog(@"%s",__PRETTY_FUNCTION__);
}
If this is is correct I need to further change this. When the view is being created it creates some subviews based on information. Also the layout is different based on a difficulty level.
Further question
How do I create a further custom init method which I pass in parameters?
For example if I created a method called
spmInitWithWord:(NSString *)word difficulty:(GameTurnDifficulty)difficulty
How is the standard init method then called. Would I then call this custom init when creating the view initially too? [[UICustomViewExample alloc] spmInitWithWord:testWord difficulty:turnDifficulty]
??