我有兩個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