问题
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