Asi*_*ike 5 uitextfield ios uitextfielddelegate appdelegate
我有一个UIView(名为HCTextFieldView)子视图:UITextField及UILabel以上.
UITextField's代表等于自我.委派方法textFieldDidBeginEditing并textFieldDidEndEditing执行文本字段的背景突出显示效果.
接下来UIView我在我的使用这个自定义(HCTextFieldView)UIViewController.要处理工具栏中的"下一步"和"上一页"按钮的操作(附在文本字段的键盘上方),我需要相同的文本字段的委托方法UIViewController,但代表被覆盖了.
**@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];
}
Run Code Online (Sandbox Code Playgroud)
设置delegate在HCBaseTextField像
在HCBaseTextField.h中添加一个属性
@property (nonatomic, assign) id<UITextFieldDelegate> textFieldDelagate;
Run Code Online (Sandbox Code Playgroud)
在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
Run Code Online (Sandbox Code Playgroud)
在ViewController中:UIViewController
...
HCTextFieldView *textFieldView = [[HCTExtFieldView alloc] init];
textFieldView.textFieldDelagate = self;
...
Run Code Online (Sandbox Code Playgroud)
并实现委托方法.
- (void) textFieldDidBeginEditing:(UITextField *)textField {
....
//Do the stuff
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
8387 次 |
| 最近记录: |