aavirya aavirya - 1 month ago 9
iOS Question

is there any way to minimize the code, as I have multiple textfields on different controllers. can I could code only once and use it

Objective C is there any way to minimize the code, as I have multiple textfields on different controllers.

Can I code only once and use it.
As I want to make the text field rectangular, show the image in the left side of the UITextFiled.

Please help if possible.
coding several time increases the size bytes of code and consumes time to, codes seems too much bulky too.

Answer

Make a sub-class of UITextField and re-use it in the proj.

YourBaseTxtField.h

@interface YourBaseTxtField : UITextField 

// Take required properties 
@end

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

In your VCs

- (void)viewDidLoad{

    self.textField = [[YourBaseTxtField alloc] init];
    self.textField.delegate = self;
    self.textField.keyboardType = UIKeyboardTypeDefault
}
// Use text field delegates

Thanks

Comments