Setup code for xibs in iOS. -awakFromNb:

30 views Asked by At

I have a UIImageView which I placed in interface builder and set to a custom class I have created SASImageView . However, when the view is loaded I want to do some setup so I have placed code in awakFromNib however, this does not seem to be called.

- (void)awakeFromNib {
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(handleTapGesture:)];
    tap.numberOfTapsRequired = 1;
    [self addGestureRecognizer:tap];
}

I set the the custom class to be SASImageView which is a subclass of UIImageView

How would I do some setup for this view in code, after its been loaded from interface builder?

Thanks.

1

There are 1 answers

0
rebello95 On

This is something you can easily put in the initWithCoder: method since you're loading from an XIB file:

-(id)initWithCoder:(NSCoder *)aDecoder {

    self = [super initWithCoder:aDecoder];
    if (self) {

        //Add customizations here
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(handleTapGesture:)];
        tap.numberOfTapsRequired = 1;
        [self addGestureRecognizer:tap];
    }
    return self;
}

Sidenote: Setting numberOfTapsRequired to 1 is unnecessary, as that's the default.