views:

321

answers:

2

I have a pickerview pulling from a datasource. I have code to update a label in the didSelectRow function, but the label is not updating. When I print the value to the NSLog, the proper value is printed. Is there something special I need to do to hookup the label so that it updates when the didSelectRow is eneter?

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
if (pickerView.tag == TagLensPicker){
    [self lensArrayData];
    label.text = [NSString stringWithFormat:@"%@",[description objectAtIndex:[pickerView selectedRowInComponent:0]]];
    NSLog([NSString stringWithFormat:@"%@", [description objectAtIndex:[pickerView selectedRowInComponent:0]]]);

}

}

+1  A: 
[pickerView selectedRowInComponent:0]

might be the source of your problem.

[description objectAtIndex:row] should work

Henrik P. Hessel
He is getting the correct value ("When I print the value to the NSLog, the proper value is printed."), so it probably isn't the source of the problem.
Adam Woś
Either of these two codes work for updating the didSelectRow, I have the information printing to NSLog and I can also display the data in a UIAlert message, but I can get the data "pushed" to my label. Any suggestions?
Michael
Well, yes, I suggested that label may not be a correct reference - see my answer somewhere around here.
Adam Woś
@Henrik: Retaining the text is incorrect! `UILabel text` is `@property(nonatomic, copy) NSString *text` - it's copied anyway, and you'll only be leaking memory by not releasing it afterwards!
Adam Woś
yeah, you're right adam.
Henrik P. Hessel
and yes. Maybe the label isn't wired up correctly in interface builder.
Henrik P. Hessel
Adam, what does the code @property (nonatomic,copy) NSString *text; do? I am new to Objective-C from Visual Basic and this aspect of setting up variables is foreign to me. I try to define the label in the .h file with and without the @property command (and the corresponding @synthesize in the .m file), but nothing works. The label is updated if I use [label setText:] in an IBAction, so I believe it is connected correctly, but I cannot get it to dynamically update while the pickerView UIView is selected. I am trying to mimic the PickerView in UICatalog.
Michael
Take a look at this tutorial: http://theocacao.com/document.page/510 - it explains properties quite well.
Adam Woś
A: 

If NSLog prints the correct value, there must be a problem with the label variable.

Print it using NSLog, see if it's a correct reference to your label?

Also, you may try calling [label setNeedsDisplay] after changing the text, although I'm not sure it's necessary.

Adam Woś
I have the label defined in the .h file as IBOutlet UILabel *label;I then connected the label to the "Outlets" tab in IB under the Connections tab for the class the label is defined in. Then, in the .m file, I call the code I wrote above in the didSelectRow. I tried to copy verbatum the UICatalog example, except my label is built in IB. I try to use both the @property command and without it (I still don't understand what @propery does). Do I have the label connected wrong in IB builder? If I update the text with an IBAction, then the label is updated correctly (but not dynamically)
Michael
Your label should be `@property (nonatomic, retain) IBOutlet UILabel *label;` Then, in the implementation of the view, use `@synthesize label;`. In the code you pasted, before `label.text = ...`, try `NSLog(@"%@", label)` and post what you got.
Adam Woś