我使用以下代码通过循环检查文本字段是否为空。 下面是我的代码,它给出了以下错误。
-[UIImageView text]: unrecognized selector sent to instance 0x997c930
2014-03-19 16:54:49.227 IS [2837:a0b] * 由于未捕获的异常'NSInvalidArgumentException'终止应用程序,原因:' - [UIImageView text]:无法识别的选择器发送到实例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++;
}
}
我没有得到我错的地方,请指导以上。它在if条件下崩溃并出现上述错误。
答案 0 :(得分:0)
此处k
是integer
value
为0
的{{1}}变量:
但是当你获取[self.view viewWithTag:k]
时,它会UIImageView
而不是UILabel
。 Text property
属UILabel
而非UIImageView
使用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 tag
至UILabel
,如999 or 9999 or 99999
。