textViewDidBeginEditing在IOS 9中不起作用

时间:2017-01-19 01:47:13

标签: ios objective-c iphone uitextfield

我是Objective C的新成员。我要做的事情很简单,只需在文本字段聚焦时为键盘设置动画,然后在不支持时缩小。我已经按照其他一些教程介绍了如何逐步设置它但它不起作用。当textfeld成为焦点时,事件textViewDidBeginEditing永远不会被调用。

@interface ViewController : UIViewController<UITextFieldDelegate>

@property (weak, nonatomic) IBOutlet UITextField *cCodeTextField;

@property (weak, nonatomic) IBOutlet UITextField *jidTextField;

- (IBAction)nextBtnTapped:(id)sender;
@end

我也将代表设置为文本字段,但它无效。

- (void)viewDidLoad {
    [super viewDidLoad];

    [self addTextFieldBorder:_jidTextField];
    [self addTextFieldBorder:_cCodeTextField];

    _jidTextField.delegate = self;
}

-(void) textViewDidBeginEditing:(UITextView *)textView {
    NSLog(@"Enter");
}

1 个答案:

答案 0 :(得分:1)

看起来你的ViewController类正在从错误的委托中实现方法。当它应该实现 UITextFieldDelegate 方法时,它正在实施 UITextViewDelegate 方法。

请注意,'jidTextField'的类型为 UITextField

您的委托方法称为“textViewDidBeginEditing”,它是 UITextViewDelegate 方法,并以 UITextView 作为参数。

这里的问题是您的班级正在为 UITextViewDelegate 而不是 UITextFieldDelegate 实施委托功能。

正确的方法定义是:

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

以下是指向正确委托的文档的链接: https://developer.apple.com/reference/uikit/uitextfielddelegate

希望这有帮助!