When I make an object to use in NIB, the object should implement -initWithCoder:. However I can’t figure out regular pattern implementing this.
I have been used this code.
- (id) initWithCoder:(NSCoder *)aDecoder
{
if(self=[super initWithCoder:aDecoder])
{
// initialize my object.
}
return self;
}
But I have to make same code for UIView instantiated by code like this.
- (id) initWithCoder:(NSCoder *)aDecoder
{
if(self=[super initWithCoder:aDecoder])
{
// initialize my object.
}
return self;
}
- (id) initWithFrame:(CGRect)frame
{
if(self=[super initWithFrame:frame])
{
// initialie my obejct.
}
return self;
}
I feels something wrong. Any recommendations?
*Of source the initialization logic can be extracted into a new method, but it’s not a point.
Use awakeFromNib to initialize a UIView that’s being created from a NIB file. If you want to make it general so that it can be created either from a NIB file or programmatically, make a method like configureObject and call it from both the designated initializer (often initWithFrame:) and from awakeFromNib.