1

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
1
  • I'm new to iOS so please forgive me for such question. Does really UIView have initWithNibName: bundle initializer? as you've used it in your code snippet. Commented Jan 13, 2013 at 23:25

3 Answers 3

4

The initializer that's used when loading a view from a nib is -initWithCoder:, not -init. From the UIView reference page:

initWithCoder:—Implement this method if you load your view from an Interface Builder nib file and your view requires custom initialization.

Moreover, if you're instantiating the view programmatically, the usual initializer is -initWithFrame:.

So, change your -init method to -initWithCoder:, or implement -initWithCoder: such that it calls your -init.

Sign up to request clarification or add additional context in comments.

4 Comments

Can you explain how to use/override this methos?
Thanks a lot one more thing, If I want to be able to init with code and from nib, am I suppose to implement both methods?
The typical thing is to create your own method that has all the initialization code that's common to both -initWithCoder: and -initWithFrame:. You can then call that method from both places so that it runs no matter how you instantiate the view.
1

Caleb is right, implement it like so :

-(id)initWithCoder:(NSCoder *)aDecoder {
    self = [super initWithCoder:aDecoder];
    if(self) {
        self.userInteractionEnabled = YES;
        // other stuff
    }
    return self;
}

1 Comment

your -(id)init; method will never get called, do your stuff in -(id)initWithCoder:(NSCoder *)aDecoder;. Your view is 'deserialized' from the nib file.
0

I though awakeFromNib was safer as all outlets wired up correctly

-(id)initWithCoder:(NSCoder *)aDecoder {
    self = [super initWithCoder:aDecoder];
    if(self) {
        //self.userInteractionEnabled = YES;
        // other stuff
    }
    return self;
}

- (void)awakeFromNib
{
    ..setup view
}

Called in order:

initWithCoder
awakeFromNib

1 Comment

Can you write how to call initWithCoder with the parameter?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.