2014-03-19 40 views
0

我使用下面的代碼來檢查文本字段是否爲空或不循環。 下面是我的代碼,它給出了以下錯誤。通過循環找到空文本字段

-[UIImageView text]: unrecognized selector sent to instance 0x997c930 

2014年3月19日16:54:49.227 IS [2837:A0B] *終止應用程序由於未捕獲的異常 'NSInvalidArgumentException',原因:「 - [文本的UIImageView]:無法識別的選擇發送到實例0x997c930'

//code 

NSString *strText; 
NSMutableArray *arrMsgs = [[NSMutableArray alloc] init]; 
[arrMsgs addObject:@"*Manifest Type is reuired."]; 
[arrMsgs addObject:@"*Suffix Code is required."]; 
[arrMsgs addObject:@"*Aircraft Registry is required."]; 
[arrMsgs addObject:@"*Flight Number is required."]; 
[arrMsgs addObject:@"*Embarkation ICAO/IATA required."]; 
[arrMsgs addObject:@"*ETD date (Local) is required."]; 
[arrMsgs addObject:@"*ETD Time (Local) is required."]; 
[arrMsgs addObject:@"*ETA date (Local) is required."]; 
[arrMsgs addObject:@"*ETA Time (Local) is required."]; 

NSLog(@"array:%d", [arrMsgs count]); 
for(int i=1; i<=9;i++) 
{ 
    UITextField *tf= (UITextField *)[self.view viewWithTag:i]; 
    int k=0; 
    UILabel *lbl = (UILabel *)[self.view viewWithTag:k]; 
    if([tf.text length] == 0) 
    { 
     strText = [arrMsgs objectAtIndex:i-1]; 
     lbl.text = strText; 
     NSLog(@"strText1:%@", strText); 
     k++; 
    } 
} 

我沒有得到我錯在哪裏,請指導以上。它在上述錯誤的條件下崩潰。

+1

我認爲你已經爲你的imageview設置了相同的標籤,你已經應用到你的textfields.Just給所有對象提供唯一標籤 – NiravPatel

+0

@NiravPatel是的你是對的。 –

回答

0

這裏您kinteger變量,其value0

但是當u取[self.view viewWithTag:k]它給UIImageViewUILabelText property是的,而不是UIImageView

UILabel檢查其UILabel或不使用iskindOfClass這樣的:

id viewTag = [self.view viewWithTag:k]; 
if([viewTag iskindOfClass:[UIImageView class]]) 
    NSLog(@"image view"); 
else if([viewTag iskindOfClass:[UILabel class]]) 
    NSLog(@"label"); 
else if([viewTag iskindOfClass:[UITextField class]]) 
    NSLog(@"txtField"); 
else 
    NSLog(@"%@",viewTag); 

解決方案add unique tagUILabel999 or 9999 or 99999