我有两个textFields.I din't全球宣称。所以它的里面我的UIAlertController
。现在textFields
是:如何获取文本字段的值,并通过该文本字段的标记值调用它们
text.tag = 100
text2.tag = 200
所以我有一个选择方法为textfield2
在该方法中,我需要调用textfield1
值具有字符串(你好)或not.Here我的代码:
问题编辑 -
我有两个textFields.I din't全球宣称。所以它的里面我的UIAlertController
。现在textFields
是:如何获取文本字段的值,并通过该文本字段的标记值调用它们
text.tag = 100
text2.tag = 200
所以我有一个选择方法为textfield2
在该方法中,我需要调用textfield1
值具有字符串(你好)或not.Here我的代码:
问题编辑 -
不喜欢
选择-1
个,您可以访问直接标签
-(void)textDidChange:(UITextField *)textField
{
if (textField.text.length > 0) {
if (textField.tag == 100) // textField1
{
if ([textField.text containsString:@"@"])
NSLog(@"Valid");
else
NSLog(@"Invalid");
}
else
{
//textField2 work
}
}
}
选择-2
,您可以访问通过对象名称
-(void)textDidChange:(UITextField *)textField
{
if (textField.text.length > 0) {
if (textField == textField1) // 100
{
if ([textField.text containsString:@"@"])
NSLog(@"Valid");
else
NSLog(@"Invalid");
}
else
{
//textField2 work
}
}
}
选择-3
我有两个文本域,我没有全局声明。 - 你的概念,然后不喜欢
- (void)textDidChange:(UITextField *)sender
{
UIAlertController *alertController = (UIAlertController *)self.presentedViewController;
if (alertController)
{
UITextField *txt1 = alertController.textFields.firstObject;
UITextField *txt2 = alertController.textFields.lastObject;
if (textField.text.length > 0) {
if (textField == txt1) // 100
{
if ([textField.text containsString:@"@"])
NSLog(@"Valid");
else
NSLog(@"Invalid");
}
else
{
//textField2 work
}
}
}
请不要这样
-(void)textDidChange:(UITextField *)textField
{
if(textField.tag == 100){// do stuff here
}else if(textField.tag == 200){
}
if (textField.text.length > 0) {
NSString *string = textField.text; // Here i have to call my first textfield value using tag value
if ([string containsString:@"@"]) {
NSLog(@"Valid");
} else {
NSLog(@"Invalid");
}
}
}
你可以尝试这样的创建UITextField
类型的一个实例变量,并与您alertController
文本框的初始化这个文本框。
UITextField txtField;
[alertController addTextFieldWithConfigurationHandler:^(UITextField *textField1) {
textField1.placeholder = @"iCloud ID";
txtField = textField1;
}];
现在使用这个textField
你的方法里面textField2
-(void)textDidChange:(UITextField *)textField {
if (textField.text.length > 0) {
NSString *string = txtField.text; // Here you will get first textfield value
if ([string containsString:@"@"]) {
NSLog(@"Valid");
} else {
NSLog(@"Invalid");
}
}
}
可以使用textFields
物业
由警报显示的文本字段的数组。 (只读)
请您尝试一下:
-(void)textDidChange:(UITextField *)textField
{
if (textField.text.length > 0) {
for (UITextField *alertTextField in alertController.textFields) {
if(alertTextField.tag == 100){ // first textfield
NSString *string = alertTextField.text;
if ([string containsString:@"@"]) {
NSLog(@"Valid");
} else {
NSLog(@"Invalid");
}
break;
}
}
}
}
@mack如果你的问题已经解决了? – ocarol
'TextField1'增加了没有目标,所以这是行不通的 –
检查更新的答案 –
将检查感谢您的解决方案 – mack