user594161
user594161

Reputation:

How Do I Format UILabel To Display From 2 UIPickerView Components?

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.

Upvotes: 0

Views: 1024

Answers (2)

Ravin
Ravin

Reputation: 8564

- (void)pickerView:(UIPickerView *)thePickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component 
{
    if (thePickerView.tag==1)
    {
       NSMutableString *labelText = [[NSMutableString alloc] initWithString:@""];
        if (component == 0)
        {
            NSLog(@"Selected weight 1: %@. Index of  array: %i", [pickerArray objectAtIndex:row], row);

            [labelText appendFormat:@"%@", [pickerArray objectAtIndex:row]];

        }
        else
        {
            NSLog(@"Selected weight 2: %@. Index of  array: %i", [pickerArrayHalf objectAtIndex:row], row);

           [labelText appendFormat:@"%@", [pickerArrayHalf objectAtIndex:row]];

        }
        weightLabel2.text = labelText;
        [labelText release];
    }

Updated :

Declare two instance variable in your .h file I am naming and using them as follows

NSMutableString *firstComponentText;
NSMutableString *secondComponentText;

alloc init above mutable strings in viewDidLoad as [[NSMutableString alloc]initWithString:@""];

- (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);

            [firstComponentText setString:[pickerArray objectAtIndex:row]];

        }
        else
        {
            NSLog(@"Selected weight 2: %@. Index of  array: %i", [pickerArrayHalf objectAtIndex:row], row);

           [secondComponentText setString:[pickerArrayHalf objectAtIndex:row]];

        }
        weightLabel2.text = [firstComponentText stringByAppendingFormat:@" %@",secondComponentText];

    }

Upvotes: 1

saadnib
saadnib

Reputation: 11145

You can append your string when the row in second column is selected by using this

weightSelected = [weightSelected stringByAppendingFormat:@" %@", [pickerArrayHalf objectAtIndex:row]];

and then set it to a single UILabel.

Upvotes: 1

Related Questions