在文本字段中输入一个字符时返回键盘

发布于 2025-01-03 12:13:03 字数 84 浏览 2 评论 0原文

我正在开发一个 iPhone 应用程序,只要我在文本字段中只输入一个字符,我就必须返回键盘。如何实现这一目标,请提出一些解决方案。

谢谢。

I am developing an iphone application in which i have to return keyboard as soon as i type only one character in textfield. How to achieve this please suggest some solution.

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

长发绾君心 2025-01-10 12:13:03

步骤 1:创建一个实现 UITextFieldDelegate 协议的类

@interface TheDelegateClass : NSObject <UITextFieldDelegate>

步骤 2:在您的实现中,重写方法 - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    // newString is what the user is trying to input.
    NSString *newString = [textField.text stringByReplacingCharactersInRange:range withString:string];
    if ([newString length] < 1) {
        // If newString is blank we will just ingore it.
        return YES;
    } else
    {
        // Otherwise we cut the length of newString to 1 (if needed) and set it to the textField.
        textField.text = [newString length] > 1 ? [newString substringToIndex:1] : newString;
        // And make the keyboard disappear.
        [textField resignFirstResponder];
        // Return NO to not change text again as we've already changed it.
        return NO;
    }
}

步骤 3:设置实例委托类的代理作为 UITextField 的委托。

TheDelegateClass *theDelegate = [[TheDelegateClass alloc] init];
[theTextField setDelegate:theDelegate];

Step 1: Create a class implementing the protocol UITextFieldDelegate

@interface TheDelegateClass : NSObject <UITextFieldDelegate>

Step 2: In your implementation, override the method - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    // newString is what the user is trying to input.
    NSString *newString = [textField.text stringByReplacingCharactersInRange:range withString:string];
    if ([newString length] < 1) {
        // If newString is blank we will just ingore it.
        return YES;
    } else
    {
        // Otherwise we cut the length of newString to 1 (if needed) and set it to the textField.
        textField.text = [newString length] > 1 ? [newString substringToIndex:1] : newString;
        // And make the keyboard disappear.
        [textField resignFirstResponder];
        // Return NO to not change text again as we've already changed it.
        return NO;
    }
}

Step 3: Set an instance of the delegate class as the delegate of the UITextField.

TheDelegateClass *theDelegate = [[TheDelegateClass alloc] init];
[theTextField setDelegate:theDelegate];
跨年 2025-01-10 12:13:03

您必须在 的 文本委托方法中编写代码

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
   if([textField.text length] == 1){
    [textField resignFirstResponder];
}

,然后在 textFieldDidBeginEditing 中检查字符串的长度

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

    if([textField.text length] == 1){
    [textField resignFirstResponder];
}

}

you have to write your code in text delegate method of

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
   if([textField.text length] == 1){
    [textField resignFirstResponder];
}

and then check your length of string in textFieldDidBeginEditing

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

    if([textField.text length] == 1){
    [textField resignFirstResponder];
}

}
做个少女永远怀春 2025-01-10 12:13:03

在文本字段中添加通知创建代码

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changeText:) name:UITextFieldTextDidChangeNotification object:textField];

并实施

- (void) changeText: (id) sender;
{
    if ([textField.text length] == 1) 
    {
        [textField resignFirstResponder];
    }        
}

add notification in textField creating code

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changeText:) name:UITextFieldTextDidChangeNotification object:textField];

and implement

- (void) changeText: (id) sender;
{
    if ([textField.text length] == 1) 
    {
        [textField resignFirstResponder];
    }        
}
行至春深 2025-01-10 12:13:03

我想这就是你正在寻找的东西?

- (BOOL)textFieldShouldReturn:(UITextField *)textField 
{
   [textField resignFirstResponder];
   [add your method here];
    return YES;

}

或者,如果您希望它在开始编辑时立即退出,您可以将此代码放入 textFieldDidBeginEditing: delegate 方法中

[textField resignFirstResponder];

,检查此链接

https://developer.apple.com/library/ios/#documentation/uikit/reference/UITextFieldDelegate_Protocol/UITextFieldDelegate/UITextFieldDelegate.html

I guess this is want you are looking for?

- (BOOL)textFieldShouldReturn:(UITextField *)textField 
{
   [textField resignFirstResponder];
   [add your method here];
    return YES;

}

Or if you want it to resign as soon as it starts to edit you can put this code in textFieldDidBeginEditing: delegate method

[textField resignFirstResponder];

check this link

https://developer.apple.com/library/ios/#documentation/uikit/reference/UITextFieldDelegate_Protocol/UITextFieldDelegate/UITextFieldDelegate.html

少女的英雄梦 2025-01-10 12:13:03
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
   if([textField.text length] == 1){
       [textField resignFirstResponder];
}

- (BOOL)textFieldShouldEndEditing:(UITextField *)textField
{
   if([textField.text length]==1)
   {
       // here perform the action you want to do
   }

}
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
   if([textField.text length] == 1){
       [textField resignFirstResponder];
}

- (BOOL)textFieldShouldEndEditing:(UITextField *)textField
{
   if([textField.text length]==1)
   {
       // here perform the action you want to do
   }

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文