2016-08-12 60 views

回答

1

不喜欢

选择-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 
     }  

    } 
} 
+0

'TextField1'增加了没有目标,所以这是行不通的 –

+0

检查更新的答案 –

+0

将检查感谢您的解决方案 – mack

1

请不要这样

-(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"); 
       } 



} 

} 
+0

'TextField1'没有添加目标,因此这不起作用 –

+0

将检查感谢您的解决方案 – mack

+0

@mack是的,请检查并更新我,如果它不工作...:) –

1

你可以尝试这样的创建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"); 
     } 
    } 
} 
+0

你在哪里使用过这个'NSString text1' –

+0

它到了他想要使用的地方 –

+0

检查你的解决方案感谢 – mack

0

可以使用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; 
      } 
     } 
    } 
} 
+0

@mack如果你的问题已经解决了? – ocarol

相关问题