iOS Custom UITextField with outlet initialization

2019-07-18 02:46发布

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?

1条回答
Explosion°爆炸
2楼-- · 2019-07-18 03:08

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;
}
查看更多
登录 后发表回答