2016-08-12 47 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

相關問題