Ilea Cristian
Ilea Cristian

Reputation: 5831

Using existing custom UIView in interface builder

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?

Upvotes: 2

Views: 948

Answers (2)

Paul.s
Paul.s

Reputation: 38728

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
}

Upvotes: 2

delannoyk
delannoyk

Reputation: 1224

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

- (id) initWithCoder:(NSCoder *)aCoder

Upvotes: 0

Related Questions