iOS Custom UITextField with outlet initialization

我是研究僧i 提交于 2019-12-12 22:02:00

问题


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:


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;
}


来源:https://stackoverflow.com/questions/25179229/ios-custom-uitextfield-with-outlet-initialization

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!