我有四个 UITextField,我想在 UIAlertView 中使用,目前它是这样的
一旦达到 4 个字符限制,我希望能够将每个框限制为 4 个字符,然后我希望下一个 UITextField 成为第一响应者。
我也希望能够反向执行此操作,因此如果在第二个字段中没有可用字符后删除字符,请转到第一个并开始删除等。
目前我的代码非常草率,因为我只是想让这样的东西工作..所以这就是它到目前为止的样子。
//提示用户在此处输入注册 UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Please Register Device"message:@""delegate:nil cancelButtonTitle:@"OK"otherButtonTitles:nil];
UITextField *myTextField = [[UITextField alloc] initWithFrame:CGRectMake(12, 45, 53, 30)];
UITextField *myTextField1 = [[UITextField alloc] initWithFrame:CGRectMake(77, 45, 53, 30)];
UITextField *myTextField2 = [[UITextField alloc] initWithFrame:CGRectMake(142, 45, 53, 30)];
UITextField *myTextField3 = [[UITextField alloc] initWithFrame:CGRectMake(207, 45, 53, 30)];
// need to do something with first reasponder once 4 digits has been entered per box etc.
[myTextField becomeFirstResponder];
myTextField.placeholder =@"AAAA";
myTextField.autocapitalizationType = UITextAutocapitalizationTypeAllCharacters;
myTextField.textAlignment = NSTextAlignmentCenter;
myTextField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
myTextField1.placeholder =@"AAAA";
myTextField1.autocapitalizationType = UITextAutocapitalizationTypeAllCharacters;
myTextField1.textAlignment = NSTextAlignmentCenter;
myTextField1.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
myTextField2.placeholder =@"AAAA";
myTextField2.autocapitalizationType = UITextAutocapitalizationTypeAllCharacters;
myTextField2.textAlignment = NSTextAlignmentCenter;
myTextField2.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
myTextField3.placeholder =@"AAAA";
myTextField3.autocapitalizationType = UITextAutocapitalizationTypeAllCharacters;
myTextField3.textAlignment = NSTextAlignmentCenter;
myTextField3.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
[myTextField setBackgroundColor:[UIColor whiteColor]];
[myTextField1 setBackgroundColor:[UIColor whiteColor]];
[myTextField2 setBackgroundColor:[UIColor whiteColor]];
[myTextField3 setBackgroundColor:[UIColor whiteColor]];
[alert addSubview:myTextField];
[alert addSubview:myTextField1];
[alert addSubview:myTextField2];
[alert addSubview:myTextField3];
[alert show];
如有任何帮助,我们将不胜感激。
//更新: 这就是我尝试使用委托(delegate)方法的方式
//.h
<UITextFieldDelegate>
//.m
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
//..
[self.myTextField setDelegate:self];
[self.myTextField1 setDelegate:self];
[self.myTextField2 setDelegate:self];
[self.myTextField3 setDelegate:self];
//..
}
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
NSLog(@"yay");
return NO;
}
最佳答案
更新
我尝试了下面的代码,确实有效,但出于某种原因,如果您更改 FirstResponder,文本将不会被写入。所以我修改了函数。你应该这样做:
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
if(textField.text.length >= MAX_LENGTH)
return NO;
if((textField.text.length + string.length) >= MAX_LENGTH)
{
int currentTag = textField.tag;
if(currentTag == 4)
return YES;
UITextField *newTextField = (UITextField *) [textField.superview viewWithTag:(currentTag+1)];
[newTextField becomeFirstResponder];
textField.text = [textField.text stringByAppendingString:string];
}
return YES; //you probably want to review this...
}
无论如何,当你不想删除内容时这不起作用,但如果你更改代码以检查更改范围,这很容易解决。
我做了一个示例项目,你可以在这里得到它:TextFieldTest
这就是我的做法,使用文本字段委托(delegate),您可以检查新插入的字符,如果达到最大值,则移至下一个文本字段:
static int MAX_LENGTH = 4;
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
if((textField.text.length + string.length) >= MAX_LENGTH)
{
//Get next TextField... A simple way to do this:
UITextField *newTextField = [textField.superview viewWithTag:(textField.tag+1)];
[newTextField becomeFirstResponder];
//remember to set the tags in order
}
return YES; //you probably want to review this...
}
(不好意思编译不了我直接写在这里)
祝你好运!
关于iphone - 如何遍历多个 UITextField,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17538583/