I have a UIPickerView with 2 components.
I want a UILabel to display the selected row. Right now I have 2 UILabels side-by-side but I think it would make more sense to have one to display the data.
The UIPickerView’s 1st component is array of numbers 1-500, the 2nd component is array of strings just “.0lb, and 1/2lb”.
pickerArray = [[NSMutableArray alloc] initWithCapacity:700];
for ( int i = 0 ; i <= 1000 ; ++i)
[pickerArray addObject:[NSString stringWithFormat:@"%d", i]];
pickerArrayHalf = [[NSMutableArray alloc]initWithCapacity:2];
[pickerArrayHalf addObject:@".0 lb"];
[pickerArrayHalf addObject:@"1/2 lb"];
I am currently using this code to feed into the 2 UILabels:
- (void)pickerView:(UIPickerView *)thePickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
if (thePickerView.tag==1)
{
if (component == 0)
{
NSLog(@"Selected weight 1: %@. Index of array: %i", [pickerArray objectAtIndex:row], row);
NSString *weightSelected;
weightSelected = [NSString stringWithFormat:@"%@", [pickerArray objectAtIndex:row]];
weightLabel.text = weightSelected;
}
else
{
NSLog(@"Selected weight 2: %@. Index of array: %i", [pickerArrayHalf objectAtIndex:row], row);
NSString *weightSelected;
weightSelected = [NSString stringWithFormat:@"%@", [pickerArrayHalf objectAtIndex:row]];
weightLabel2.text = weightSelected;
}
}
The idea for the 1 UILabel over the 2 currently is because this string should be a number, like “50.0” or “200 1/2”, etc.
Updated :
Declare two instance variable in your .h file I am naming and using them as follows
alloc init above mutable strings in
viewDidLoadas[[NSMutableString alloc]initWithString:@""];