Question

I am trying to show the UIPickerView with a selected value according to the value of a label in the view.

For example if the label value is 2 and when I open the pickerView I want the value two to be selected in the picker. How would this happen?

My code:

-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{

  //res is array
  return [[res objectAtIndex:row]objectForKey:@"choice_name"];
}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
    //choiceList is sting
    self.choiceList = [NSString stringWithFormat:@"<r_PM act='closepos_choice' cl_choice='%d'/>", row];
}

 - (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view {

UILabel *pickerLabel = (UILabel *)view;
if (pickerLabel == nil) {
    CGRect frame = CGRectMake(0.0, 0.0, 300, 30);
    pickerLabel = [[[UILabel alloc] initWithFrame:frame] autorelease];
    [pickerLabel setTextAlignment:UITextAlignmentCenter];
    [pickerLabel setBackgroundColor:[UIColor clearColor]];
    [pickerLabel setFont:[UIFont fontWithName:@"HelveticaNeue" size:16.0]];

    //EDITED CODE FOR C_X
    NSNumberFormatter * f = [[NSNumberFormatter alloc] init];
    [f setNumberStyle:NSNumberFormatterDecimalStyle];
    NSNumber * myNumber = [f numberFromString:lblOtomatikPozKap.text];
    [f release];

    NSInteger index = [res indexOfObject:myNumber];

    if(index != NSNotFound ){

        [self.pickerView selectRow:index inComponent:0 animated:NO];
    }

    //EDITED FOR AUSTIN
    NSInteger indexOfItem = [res indexOfObjectPassingTest:^BOOL(id obj, NSUInteger idx, BOOL *stop) {
    return [[obj objectForKey:@"choice_name"] isEqualToString:@"2"];
    }];

    if (indexOfItem != NSNotFound)
    {
    [self.pickerView selectRow:indexOfItem inComponent:1 animated:NO];
    }
 }

NSString *str = [NSString stringWithFormat:@"%d- %@", row, [[res objectAtIndex:row]objectForKey:@"choice_name"]];

[pickerLabel setText:str];

return pickerLabel;
}

//Not working at all
- (void)selectRow:(UIPickerView*)row inComponent:(NSInteger)component animated:(BOOL)animated
{}
Was it helpful?

Solution

You can select a row by calling this method on pickerview selectRow:inComponent:animated: Edit:

You are getting strings from res, first you have to add label string to this array so you can have this row in pickerView. After that in viewWillAppear or any other method where you want call this method. First get index of that sring/object.

NSInteger index=[resindexOfObject:label.text];

Now select the row.

if(index != NSNotFound ){
 [self.myPicker selectRow:index inComponent:0 animated:NO]
}

OTHER TIPS

First, you would get the index of the value you want to select from your data source (the res array, in this case). Then, you would call selectRow:inComponent:animated: before showing the picker:

NSInteger indexOfItem = [res indexOfObjectPassingTest:^BOOL(id obj, NSUInteger idx, BOOL *stop) {
    return [[obj objectForKey:@"choice_name"] isEqualToString:@"2"];
}];

if (indexOfItem != NSNotFound)
{
    [self.pickerView selectRow:indexOfItem inComponent:1 animated:NO];
}

// Show picker here
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top