Search code examples
iosobjective-cuitableviewuilabelnslog

How to copy UITableViewCell label to UILabel in nextView?


I just want to copy UITableViewCell's label (which is a simple string) into nextView's UIlabel. I tried creating a string property in the nextView and passing it the cell label, but it doesn't work. I'm getting nil in nextView, why is that? Here is my didSelectRowAtIndexPath Method in rootViewController.m

    -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
    {
        NSString *selLabel =[tempArray objectAtIndex:indexPath.row];
        DetailViewController *detailViewCont=[[DetailViewController alloc]initWithNibName:@"DetailViewController" bundle:nil];
detailViewCont.selectedLabel=selLabel;        
[self.navigationController pushViewController:detailViewCont animated:YES];

      NSLog(@"selected Label %@",detailViewCont.selectedLabel);
    }

Last NSlog statement returns the correct string here.

In nextViewController.m

-(void)viewDidLoad
{
[super viewDidLoad];
selectedLabel=[[NSString alloc]init];

UILabel *label1=[[UILabel alloc]init];
    label1.frame=CGRectMake(5,5,310, 60);
    label1.font=[UIFont fontWithName:@"Arial Black" size:20];
    label1.text=selectedLabel;
    NSLog(@"sellabel %@",selectedLabel);
 [self.View addSubview:label1];
}

NSLog statement here returns null


Solution

  • Delete the following line from you viewDidLoad method:

    selectedLabel=[[NSString alloc]init];
    

    This is clearing the value you set from the other view controller.

    And this line:

    label1.text=selectedLabel;
    

    should really be:

    label1.text = self.selectedLabel;
    

    You setup a property, use it.