new2ios
new2ios

Reputation: 1350

iOS Custom UITextField with outlet initialization

I need UITextField with little customization. I've created new custom class CustomTextField. In IB I created outlet for each of my custom UITextField. I run the code and customization was not working.

To make check I put breakpoint on initialization procedure initWithFrame in the custom UITextField. The breakpoint was not hit. I suppose that additional init must be made in each View Controller, but I am not sure what exactly.

As I created outlet I suppose that init will be made automatically, but because this is a custom class it must be init manually with construction like that:

CustomTexTField *textField = [[CustomTextField alloc] initWithFrame:customFrame];
[self.view addSubView:textField];

So do I need additional init in each View controller and how to do that?

Upvotes: 2

Views: 1195

Answers (1)

bobnoble
bobnoble

Reputation: 5824

When initializing a view from IB or a storyboard, initWithCoder is the initializer used, not initWithFrame, and is why the breakpoint is not being hit. Put the customization code in the initWithCoder method. E.g.:

- (id)initWithCoder:(NSCoder *)aDecoder{
    self = [super initWithCoder:aDecoder];
    if (self) {

        // Put customization code here...

    }
    return self;
}

Upvotes: 2

Related Questions