0

Hello Im sure you all can answer this but its bugging the hell out of my because i am being stupid.

I have an array and can store the didSelectRowAtIndexPath row and then NSLog formatSelected. I then pop the view controller and desplay the formatSelected as the botton title.

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    NSString *formatSelected = [[NSString alloc]init];
    formatSelected:[format objectAtIndex:indexPath.row];
    NSLog(@"this is the format selected %@",formatSelected);
    [button2 setTitle:[format objectAtIndex:indexPath.row] forState:UIControlStateNormal];
    [navigationControler popViewControllerAnimated:YES];
}

That works fine.

My problem is, in the previous view where the newly titled button is I have another button and a label.

I want to be able to press the second button and display the formatSelected String in the label or NSLog it

-(IBAction)printResults{
NSString *fmat = [[NSString alloc]initWithFormat:@"%@",formatSelected];
NSLog(@"%@",fmat);
    NSLog(@"nslong button pressed");}

But the NSLog just displays (null)?

I have @property (nonatomic, retain) NSString *formatSelected; and synthesized it.

What am I doing wrong?

user2314737
  • 27,088
  • 20
  • 102
  • 114
NewAtIOS
  • 1
  • 1

2 Answers2

1

You are declaring formatSelected as a local variable in the method tableView:didSelectRowAtIndexPath:. Anything you assign to formatSelected within this method will cease to be accessible after the method exits. You are assigning the selected format to this local variable instead of to your property (and its corresponding instance variable).

Use [self setFormatSelected:[format objectAtIndex:indexPath.row]]; and remove NSString *formatSelected... line entirely.

puzzle
  • 6,071
  • 1
  • 25
  • 30
  • Thank you for you help it works a treat......but....maybe i am stupid....... the setFormatSelected line??? formatSelected now has an uppercase 'F' how does that work? surly its changing the case sensitive formatSelected variable? how is it referencing the formatSelected if its case sensitive? – NewAtIOS Jun 29 '12 at 13:52
0

You almost always want to use copy with an NSString *, not retain. See this answer for more info: NSString property: copy or retain?

Community
  • 1
  • 1
davidgoli
  • 2,436
  • 1
  • 16
  • 13