Objective c 如何访问在Objective C中使用for循环动态创建的textField值?
我已经使用目标C中的for循环创建了4个Objective c 如何访问在Objective C中使用for循环动态创建的textField值?,objective-c,Objective C,我已经使用目标C中的for循环创建了4个UITextField。现在我想验证这些文本。这些文本字段是否为空 for(int i=0;i<4; i++){ UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(x, y, 211, 39)]; UIView *paddingView = [[UIView alloc] initWithFrame:CG
UITextField
。现在我想验证这些文本。这些文本字段是否为空
for(int i=0;i<4; i++){
UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(x, y, 211, 39)];
UIView *paddingView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 10, 20)];
[textField setTag:i];
textField.leftView = paddingView;
textField.leftViewMode = UITextFieldViewModeAlways;
textField.backgroundColor = [UIColor colorWithRed:237.0/255.0 green:237.0/255.0 blue:237.0/255.0 alpha:1.0];
textField.font = [UIFont systemFontOfSize:15];
textField.placeholder = [placeholder objectAtIndex:i];
textField.autocorrectionType = UITextAutocorrectionTypeNo;
textField.keyboardType = UIKeyboardTypeDefault;
textField.returnKeyType = UIReturnKeyDone;
textField.clearButtonMode = UITextFieldViewModeWhileEditing;
textField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
if(i>=2){
[textField setSecureTextEntry:YES];
}
[self.view addSubview:textField];
y=y+69;
}
for(int i=0;i=2){
[文本字段设置安全扩展:是];
}
[self.view addSubview:textField];
y=y+69;
}
您可以使用标记值检索如下文本字段
UITextField *txtField1 = [self.view viewWithTag:0];
UITextField *txtField2 = [self.view viewWithTag:1];
UITextField *txtField3 = [self.view viewWithTag:2];
UITextField *txtField4 = [self.view viewWithTag:3];
UITextField *txtField5 = [self.view viewWithTag:4];
if(txtField1.text.length > 0)
{
}
您可以对其他文本字段应用相同的检查
注:
确保具有唯一的标记值,而不是0,1,2,3,4,5 如果要检查
UITextField
是否为空,请定义一个宏来修剪空白
#define TRIM(string) [string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]]
在按钮操作方法中
UITextField *textField1 = [self.view viewWithTag:0];
UITextField *textField2 = [self.view viewWithTag:1];
UITextField *textField3 = [self.view viewWithTag:2];
UITextField *textField4 = [self.view viewWithTag:3];
if(![TRIM(textField1.text) length])
{
// Your textfield is empty
}
//Similarly check for other textfields
for(int i=0;i
for(int i=0;i<4; i++) {
UITextField *textField = [self.view viewWithTag:i];
if (textField.hasText) {
//If this returns true then text field has text otherwise not
}
}