是否有任何方法可以最小化代码,因为我在不同的控制器上有多个文本字段.我可以只编码一次并使用它

0 objective-c ios

Objective C有什么方法可以最小化代码,因为我在不同的控制器上有多个文本字段.

我可以只编码一次并使用它.由于我想将文本字段设为矩形,请在UITextFiled的左侧显示图像.

请尽可能帮助.编码几次会增加代码的大小字节并消耗时间,代码似乎也太笨重了.

Sai*_*dra 5

制作一个子类UITextField并在项目中重复使用它.

YourBaseTxtField.h

@interface YourBaseTxtField : UITextField 

// Take required properties 
@end
Run Code Online (Sandbox Code Playgroud)

YourBaseTxtField.m

@interface YourBaseTxtField () 

@end

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

        //  Do customization
        self.clipsToBounds = YES;
        [self setLeftViewMode:UITextFieldViewModeAlways];

    }
 return self;
}
Run Code Online (Sandbox Code Playgroud)

在您的VC中

- (void)viewDidLoad{

    self.textField = [[YourBaseTxtField alloc] init];
    self.textField.delegate = self;
    self.textField.keyboardType = UIKeyboardTypeDefault
}
// Use text field delegates
Run Code Online (Sandbox Code Playgroud)

谢谢