Reputation:
I'm just getting to the part of my Obj-C book where init methods are covered. I understand them, but I'm not sure where to call the method. I have a VC class that needs to have one of its @property
s set to another value, besides the default 0 before the rest of the code is executed. VC is a subclass of UIViewController
.
Here is my code for the init method in VC.m:
- (id)initmethod
{
if((self = [super init])){
self.value = -1 ;
}
return self ;
}
Where do I call this? Do I need to have a sub class that calls it? Or do I need to manually create the VC object that is linked to the view and call this method there?
Upvotes: 1
Views: 2798
Reputation: 104082
Yes you need to call this when you create the controller,
VC *vcInstance = [[VC alloc] initMethod];
There's really no need to create your own init method though, you can just override init.
If you're making your controller in a storyboard however, you would want to override initWithCoder, and set your value property there.
-(id)initWithCoder:(NSCoder *)aDecoder {
if (self = [super initWithCoder:aDecoder]) {
_value = -1; // you should use the ivar rather than self.propertyName inside an init method
}
return self;
}
Likewise, if the controller is made in a xib file you should override initWithNibName:bundle:
Upvotes: 1