简体   繁体   English

编辑时返回文本框键盘

[英]return textfield keyboard on editing

I am working on iphone application in which i have to return the keyboard when i edit my textfield or delete the text in textfield also my textfield should allow only one character in textfield. 我正在iphone应用程序上工作,在该应用程序中,当我编辑文本字段或删除文本字段中的文本时必须返回键盘,而且我的文本字段应只允许文本字段中的一个字符。

Thanks 谢谢

Implement UITextFieldDelegate protocol and implement the following method 实现UITextFieldDelegate协议并实现以下方法

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string

You can get the length of the text in the textfield. 您可以在文本字段中获取文本的长度。 If it is greater than 1, then, resign the first responder. 如果它大于1,则请辞职第一响应者。

You can do something like this, considering _textField as your textfield object. 您可以考虑将_textField作为您的textfield对象,执行以下操作。

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    if( textField == _textField )
    {
        if( [textField.text length] == 0 )
        {        
            return YES;
        }
        else
        {
            [textField resignFirstResponder];
            return NO;
        }
    }
    else
    {
        return YES;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM