开发者

equivalent to textViewShouldBeginEditing for TEXTFIELD?

I have an UITextView and if the user touches it, an accessory view will be added to the keyboard, so that the user can hide the keyboard again.

I now added an UITextField and was wondering how to add the same accessory view to the keyboard once it pops upon for the UITextField. Is there an equivalent for the following code for UITextfield? textFieldShouldBeginEditing does not exist...

 - (BOOL)textViewShouldBeginEditin开发者_运维技巧g:(UITextView *)aTextView {


    if (aTextView.inputAccessoryView == nil) {
        aTextView.inputAccessoryView = accessoryView;    
        self.accessoryView = nil; // After setting the accessory view for the text view, we no longer need a reference to the accessory view
    }

    return YES;
}


There is the UITextFieldDelegate protocol in which you can find the method

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField

Have been looking for that one?


It does exist, except it's found in the UITextFieldDelegate protocol.

If your view controller manages both a text view and a text field, have the controller class adopt both UITextViewDelegate and UITextFieldDelegate.


For UITextField,your code must be as below.

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


    if (aTextField .inputAccessoryView == nil) {
        aTextField .inputAccessoryView = accessoryView;    
        aTextField.accessoryView = nil; // After setting the accessory view for the text view, we no longer need a reference to the accessory view
    }

    return YES;
}

Confirm with UITextFieldDelegate protocol for UITextField

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜