iphoneloops

Find the empty textfields through the loop


I am using the below code to check the textfields whether they are empty or not through loop. Below is my code, it is giving the following error.

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

2014-03-19 16:54:49.227 IS[2837:a0b] * Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UIImageView text]: unrecognized selector sent to instance 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++;
    }
}

I am not getting where i am wrong, please guide for above. It crashes at the if condition with above mentioned error.


Solution

  • Here your k is integer variable whose value is 0:

    But when you fetch [self.view viewWithTag:k] it gives UIImageView not UILabel. Text property is of UILabel and not of UIImageView

    Check if it's UILabel or not using iskindOfClass like this :

    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); 
    

    Solution is add unique tag to UILabel like 999 or 9999 or 99999.