最大数が 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;
MyViewController.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;
}