I have a UIView and I initialize it from a nib file.
In my nib file I dragged and dropped a UIImageView and I changed the class name to MyImage.
When i load the view it doesn’t seem like it’s initializing the the image using my custom class, because the init method is not getting called.
Any idea what the problem is?
@interface MyView : UIView
@property (nonatomic, retain) IBOutlet MyImage *image;
@end
@implementation MyView
@synthesize image;
- (id)init
{
self = [super initWithNibName:@"MyView" bundle:nibBundleOrNil];
return self;
}
@end
Here is MyImage
Here is my Image
@interface MyImage : UIImageView
@end
@implementation MyImage
- (id)init
{
// This doesn't get called
self = [super init];
if (self)
{
// do somethin
}
return self;
}
@end
The initializer that’s used when loading a view from a nib is
-initWithCoder:, not-init. From the UIView reference page:Moreover, if you’re instantiating the view programmatically, the usual initializer is
-initWithFrame:.So, change your
-initmethod to-initWithCoder:, or implement-initWithCoder:such that it calls your-init.