检查最大数字是否为 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;
}