2

检查最大数字是否为 1 的代码在我只使用那段代码时有效。但是当我使用 [becomFirstResponder] 添加自动跳转到下一个 UITextField 时,它不再起作用了。

非常感谢你们!

在此处输入图像描述 在此处输入图像描述 在此处输入图像描述

什么时候

MyViewController.h

@interface GateKeeperViewController : UIViewController <UITextFieldDelegate>

@property (strong, nonatomic) IBOutlet UITextField *firstCodeField;
@property (strong, nonatomic) IBOutlet UITextField *secondCodeField;
@property (strong, nonatomic) IBOutlet UITextField *thirdCodeField;
@property (strong, nonatomic) IBOutlet UITextField *fourthCodeField;
@property (strong, nonatomic) IBOutlet UITextField *fifthCodeField;

我的视图控制器.m

@synthesize firstCodeField;
@synthesize secondCodeField;
@synthesize thirdCodeField;
@synthesize fourthCodeField;
@synthesize fifthCodeField;

- (void)viewDidLoad
{
    [super viewDidLoad];

    [self initFieldListeners];
}

-(void) initFieldListeners {

    firstCodeField.delegate = self;
    secondCodeField.delegate = self;
    thirdCodeField.delegate = self;
    fourthCodeField.delegate = self;
    fifthCodeField.delegate = self;

    [self.firstCodeField addTarget:self
                            action:@selector(textFieldEditing:)
                  forControlEvents:UIControlEventEditingChanged];

    [self.secondCodeField addTarget:self
                             action:@selector(textFieldEditing:)
                   forControlEvents:UIControlEventEditingChanged];

    [self.thirdCodeField addTarget:self
                            action:@selector(textFieldEditing:)
                  forControlEvents:UIControlEventEditingChanged];

    [self.fourthCodeField addTarget:self
                             action:@selector(textFieldEditing:)
                   forControlEvents:UIControlEventEditingChanged];

    [self.fifthCodeField addTarget:self
                            action:@selector(textFieldEditing:)
                  forControlEvents:UIControlEventEditingChanged];
}

- (void)textFieldEditing:(UITextField *)textField
{
    if(textField == self.firstCodeField ){
        if ([self.firstCodeField.text length] == 1) {
            [self.secondCodeField becomeFirstResponder];
        }
    }

    if(textField == self.secondCodeField ){
        if ([self.secondCodeField.text length] == 1) {
            [self.thirdCodeField becomeFirstResponder];
        }
    }

    if(textField == self.thirdCodeField ){
        if ([self.thirdCodeField.text length] == 1) {
            [self.fourthCodeField becomeFirstResponder];
        }
    }

    if(textField == self.fourthCodeField ){
        if ([self.fourthCodeField.text length] == 1) {
            [self.fifthCodeField becomeFirstResponder];
        }
    }
}

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

}
4

3 回答 3

2

我建议使用稍微不同的解决方案,target-action在文本字段上使用以获取编辑事件的通知。这个想法是您的操作将在该shouldChangeCharactersInRange方法之后执行。例如使用 3 个文本字段:

- (void)viewDidLoad
{
    [super viewDidLoad];

    [self.firstTextField addTarget:self
                            action:@selector(textFieldEditing:)
                  forControlEvents:UIControlEventEditingChanged];

    [self.secondTextField addTarget:self
                             action:@selector(textFieldEditing:)
                   forControlEvents:UIControlEventEditingChanged];

}

- (void)textFieldEditing:(UITextField *)textField
{
    if(textField == self.firstTextField ){
        if ([self.firstTextField.text length] == 1) {
            [self.secondTextField becomeFirstResponder];
        }
    }

    if(textField == self.secondTextField ){
        if ([self.secondTextField.text length] == 1) {
            [self.thirdTextField becomeFirstResponder];
        }
    }
}

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

}

仍然可以对此代码进行一些改进,但它应该可以帮助您入门。

于 2014-05-31T12:08:24.630 回答
1

您在shouldChangeCharactersInRange委托获得其操作之前设置了值。

意思是,您需要删除setText.

于 2014-05-31T11:51:18.243 回答
0

删除您的 - setText: 方法并这样做

创建一个局部变量来保留最后一个 NSString 和当前 UITextField 的引用

[NSTimer scheduledTimerWithTimeInterval:0.0 target:self selector:@selector(giveText:) userInfo:nil repeats:NO]; // instead of your - setText:

- (void)giveText:(id)sender {

    // set your text here lets say

    [currentTextField setText:lastString];
}

你必须在 return 语句之后设置文本,但这是不可能的,所以你滥用 NSTimer 来做到这一点。

例子:

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

    _lastString = string;
    _currentTextField = textField;

    if(firstCodeField == textField){

        // [firstCodeField setText:newString];

        [secondCodeField becomeFirstResponder];
    }

    // ...... other ifs

    [NSTimer scheduledTimerWithTimeInterval:0.0 target:self selector:@selector(giveText:) userInfo:nil repeats:NO];
}
于 2014-05-31T12:17:11.737 回答