【问题标题】:I cannot intercept the back button of Numpad我无法拦截 Numpad 的后退按钮
【发布时间】:2014-05-31 14:07:24
【问题描述】:

当按下返回键或返回键时,我希望 firstResponder 转到前一个 TextField。但是 textFieldShouldReturn 没有启动。非常感谢你们!

试过this,但似乎不起作用。

MyViewController.h

@interface MyViewController : UIViewController <UITextFieldDelegate>

@property (strong, nonatomic) IBOutlet UITextField *firstCodeField;

MyViewController.m

@synthesize firstCodeField;

- (void)viewDidLoad
{
[super viewDidLoad];
firstCodeField.delegate = self;
secondCodeField.delegate = self;
thirdCodeField.delegate = self;
fourthCodeField.delegate = self;
fifthCodeField.delegate = self;
}

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    NSUInteger newLength = [textField.text length] + [string length] - range.length;

    if (string.length == 0 && textField.text.length)
    {
        if(textField==fifthCodeField) {
            NSLog(@"It goes to fourth textfield but doesn't clear the number in the cell.");
            [textField resignFirstResponder];
            [fourthCodeField becomeFirstResponder];
            return NO;
        }
    }

    return (newLength > 1) ? NO : YES;
}

【问题讨论】:

  • 检查文本字段的代表

标签: ios objective-c uitextfield uitextfielddelegate


【解决方案1】:

必须在textField :shouldChangeCharactersInRange:中识别退格

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
      {
        if (string.length == 0 //BackSpace
            && textField.text.length)//No Characters in text field
        {
            [textField resignFirstResponder];
            return NO;
        }
        return YES;
    }

更新的答案。当第五个字段为空时,单击退格会移动到第四个字段并清除第四个字段的最后一个字符

    - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
    {
        NSUInteger newLength = [textField.text length] + [string length] - range.length;

        if (string.length == 0 && textField.text.length)
        {
            if(textField==fifthCodeField) {
                NSLog(@"It goes to fourth textfield but doesn't clear the number in the cell.");
                [textField resignFirstResponder];

                //Character range for last character
                NSRange fourthFieldRange;
                range.length = 1;
                range.location = field.text.length - 1;

                NSString *text = [fourthCodeField.text stringByReplacingCharactersInRange:fourthFieldRange withString:@""];
                 [fourthCodeField becomeFirstResponder];
                return NO;
            }
        }

        return (newLength > 1) ? NO : YES;
    }

【讨论】:

  • 嗨,试过你的代码。它从第五个文本字段到第四个。但就是这样。它不会去第三,第二,第一。
  • 您必须将所有文本字段的委托添加为 self。如果您在此方法中设置断点,是否会在第三个和第二个文本字段中停止?
  • 嗨@Naveen Prasad R 我快到了。使用您的代码,它会转到第四个文本字段,但不会清除单元格中的数字。
  • 当用户点击第五个单元格中的后退按钮(如果第五个单元格没有文本)时,你想做什么,你希望它移动到第四个单元格并清除第四个单元格中的文本?
  • 如果您期望相同,请查找更新后的答案。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-07-25
  • 2010-12-23
  • 2017-01-13
  • 2020-07-19
  • 2017-08-23
相关资源
最近更新 更多