Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I made an UIView subclass because I needed something custom.

Then, I added an UIView on one of my xibs and changed the class of from UIView to my custom class. When I run, it doesn't instantiate the custom class.

What do I have to do? What constructor will be used to instantiate the custom class?

share|improve this question
There's no custom constructor, How are you checking that is not initiated ? – Adeel Pervaiz Apr 15 at 11:00

2 Answers

The xib will be instantiated with

- (id)initWithCoder:(NSCoder *)decoder

If you are relying on connections and other objects in the xib to be available then you may also add code to

- (void)awakeFromNib

which is when the object ... is guaranteed to have all its outlet and action connections already established.


To deal with code duplication you can do something like the following

- (instancetype)initWithFrame:(CGRect)frame
{
  self = [super initWithFrame:frame];
  if (self) {
    [self commonInit];
  }
  return self;
}

- (instancetype)initWithCoder:(NSCoder *)aDecoder
{
  self = [super initWithCoder:aDecoder];
  if (self) {
    [self commonInit];
  }
  return self;
}

- (void)commonInit
{
  // any common setup
}
share|improve this answer

The method called when you create an UIView from a XIB is

- (id) initWithCoder:(NSCoder *)aCoder
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.