Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using same UITextFieldDelegate methods in custom UIView and in UIViewController simultaneously

I have an UIView (named HCTextFieldView) with subviews: UITextField and UILabel above.

UITextField's delegate is equal to self. Delegate methods textFieldDidBeginEditing and textFieldDidEndEditing perform textfield's background highlight effect.

Next I'am using this custom UIView (HCTextFieldView) in my UIViewController. To handle action of 'Next' and 'Previous' buttons in toolbar (attached above textfield's keyboard) I need the same textfield's delegate methods in UIViewController, BUT delegates became overridden.

**@interface HCBaseTextField : UIView <UITextFieldDelegate>**
...
@end

**@implementation HCBaseTextField {}**

...

textField = [[UITextField alloc] initWithFrame:CGRectMake(0, titleLabel.bottom, self.width - 20, self.height - titleLabel.height)];
**textField.delegate = self**;

...

#pragma mark - UITextField delegate

//textFieldBG - UIImageView that act as background

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
    [textFieldBg setImage:[[UIImage imageWithName:@"btn_vvod_medium_act"] stretchableImageWithLeftCapWidth:10 topCapHeight:10]];
    return YES;
}

- (BOOL)textFieldShouldEndEditing:(UITextField *)textField {
    [textFieldBg setImage:[[UIImage imageWithName:@"btn_vvod_medium_norm"] stretchableImageWithLeftCapWidth:10 topCapHeight:10]];
    return YES;
}

...

@end


**ViewController : UIViewController**

...

HCTextFieldView *textFieldView = [[HCTExtFieldView alloc] init];
textFieldView.textField.delegate = self;

...

//I need to use this methods too but they override previous in UIView delegate
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
    [self.keyboardControls setActiveField:textField];
}

- (void)textViewDidBeginEditing:(UITextView *)textView
{
    [self.keyboardControls setActiveField:textView];
}
like image 839
Asike Avatar asked Apr 17 '14 02:04

Asike


1 Answers

Set a delegate in HCBaseTextField like

in HCBaseTextField.h add a property

@property (nonatomic, assign) id<UITextFieldDelegate> textFieldDelagate;

and in HCBaseTextField.m

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {

    ....
    if (self.textFieldDelagate && [self.textFieldDelagate respondsToSelector:@selector(textFieldShouldBeginEditing:)]) {
        [self.textFieldDelagate textFieldShouldBeginEditing:textField];
    }
    return YES;
}

- (void) textFieldDidBeginEditing:(UITextField *)textField {
    ....

    if (self.textFieldDelagate && [self.textFieldDelagate respondsToSelector:@selector(textFieldDidBeginEditing:)]) {
        [self.textFieldDelagate textFieldDidBeginEditing:textField];
    }
}

... //Other delegate methods if needed

and in ViewController : UIViewController

...

HCTextFieldView *textFieldView = [[HCTExtFieldView alloc] init];

textFieldView.textFieldDelagate = self;

...

and implement delegate methods.

- (void) textFieldDidBeginEditing:(UITextField *)textField {
    ....

    //Do the stuff
}
like image 162
Akhilrajtr Avatar answered Nov 04 '22 12:11

Akhilrajtr