当我将 UITextField 与 resignFirstResponder 结合使用时,它不是最多 1 个数字
Posted
技术标签:
【中文标题】当我将 UITextField 与 resignFirstResponder 结合使用时,它不是最多 1 个数字【英文标题】:UITextField is not a maximum of 1 number when I combine it with resignFirstResponder 【发布时间】:2014-05-31 11:41:15 【问题描述】:检查最大数字是否为 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;
【问题讨论】:
【参考方案1】:我建议一个稍微不同的解决方案,在文本字段上使用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;
仍然可以对此代码进行一些改进,但它应该可以帮助您入门。
【讨论】:
【参考方案2】:您在 shouldChangeCharactersInRange
委托执行操作之前设置了值。
意思是,您需要删除setText
。
【讨论】:
不起作用。它跳到 secondCodeField,然后是fourthCodeField。 检查您的 IBOutlet 连接和委托。【参考方案3】:删除你的 - 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];
【讨论】:
以上是关于当我将 UITextField 与 resignFirstResponder 结合使用时,它不是最多 1 个数字的主要内容,如果未能解决你的问题,请参考以下文章
当我在 iOS7 中使用物理键盘在 UITextfield 上键入时出现错误
没有看到 UIScrollView 上的 UITextField 时 resignFirstResponder