Nani
Nani

Reputation: 25

Assign values to Labels Objective-C

am new to iOS Programming, I have a issue regarding assigning values to labels, Below is what i get from a service

(
{
    EmpName = Peter;
    Relation = SouthAfrica;
},
{
    EmpName = Smith;
    Relation = WestIndies;
},
{
    EmpName = Andrew;
    Relation = England;
},
{
    EmpName = John;
    Relation = Australia;
},
{
    EmpName = Rahul;
    Relation = India;
}
)

Above i have got 5 records, my problem is how to assign each EmpName to 5 labels, like peter to Label1, Smith to Label2, Andrew to Label3 and so on. Am getting only first empName, Below is the code what i have tried.

- (void)connectionDidFinishLoading:(NSURLConnection *)connection {

    NSString *responseString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
    jsonArray=nil;
    NSLog(@"responseString is%@",responseString);
    jsonArray=[NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingMutableContainers|NSJSONReadingAllowFragments|NSJSONReadingMutableLeaves error:nil];
    profileArray=[jsonArray mutableCopy];
    NSLog(@"%lu",(unsigned long)[jsonArray count]);

    if ([jsonArray count]==0) {

        NSLog(@"responseString is%@",responseString);

        UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"Validation" message:@"Error Connecting to server" delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil];
        [alert show];

    }
    else
    {
        Label1.text = [[jsonArray objectAtIndex:0]objectForKey:@"EmpName"];

    }
}

Upvotes: 2

Views: 962

Answers (2)

Jagdeep Singh
Jagdeep Singh

Reputation: 2626

As of know in response from server there are only 5 employees, but in future these could be 100 or less or more. So you should use UITableView for this. I hope you have knowledge of UITableView.

Upvotes: 0

santak
santak

Reputation: 327

Add tags to your labels (like tag = 1 for label1, tag = 2 for label2 ..... tag = 5 in label5) then in your else part you can get all labels with their tag.

    for (int i = 0; i < jsonArray.count; i++) {
        UILabel *lbl = (UILabel *)[self.view viewWithTag:i];
        lbl.text = [[jsonArray objectAtIndex:i] objectForKey:@"EmpName"];
    }

Upvotes: 1

Related Questions