2

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
{}
Cem
  • 60
  • 7

2 Answers2

1

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]
}
Adnan Aftab
  • 14,377
  • 4
  • 45
  • 54
  • Hi C_X, could you give me some more details? Thanks. – Cem Jan 14 '14 at 14:13
  • I have tested the code above it is not working. Could help please? – Cem Jan 14 '14 at 15:58
  • How did you test this, do you add label text in array and are you put ur code in correct place? it will be better if you show your code... – Adnan Aftab Jan 14 '14 at 15:59
  • why you are adding this method, this is pickerView instance method, just use that, call that method from viewDidAppear or ViewWillAppear or after picker view load data – Adnan Aftab Jan 14 '14 at 16:17
  • Thanks C_X. I have tried both viewDidAppear or ViewWillAppear and passing inside the method but still the picker showing me the first row. – Cem Jan 14 '14 at 16:25
  • It should select, check by selecing some hard code row like [self.myPicker selectRow:2 inComponent:0 animated:NO] in ViewWillAppear. This will work if you are showing pickerView in new viewcontroller...? if you are showing in same, then you should do this after displaying pickerView – Adnan Aftab Jan 14 '14 at 16:33
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/45216/discussion-between-cem-and-c-x) – Cem Jan 14 '14 at 16:46
1

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
Austin
  • 5,625
  • 1
  • 29
  • 43
  • Hi austin, is there another way instead of showing the picker in the below according to your code? Thanks. – Cem Jan 14 '14 at 14:34
  • You don't have to show the picker where the `// Show picker here` comment is; that was just a suggestion. You could do this at any point after you've created the picker, even after you've shown it. – Austin Jan 14 '14 at 14:46
  • I am sorry it is not working. Could you help me please? – Cem Jan 14 '14 at 15:14
  • Set a breakpoint on the `if (indexOfItem != NSNotFound)` and check what the index is. Make sure that `res` isn't `nil` or something like that, too. – Austin Jan 14 '14 at 15:26
  • It is not passing inside the `indexOfItem` or `IF` – Cem Jan 14 '14 at 15:44
  • That means that a matching item was not found. Your don't want to use the hardcoded value `@"2"`, that was also an example, to match your example value in the question. EDIT: Do you mean that it isn't hitting any of this code at all, or just that it isn't hitting the inside of the `if` statement? – Austin Jan 14 '14 at 15:46
  • It in not hitting the inside of the `if` statement. Even not hitting inside `indexOfItem` – Cem Jan 14 '14 at 15:52
  • You need to put my code somewhere before the picker is presented (`viewWillAppear:`, `viewDidAppear:`, etc), and not in the delegate method. – Austin Jan 14 '14 at 16:21
  • I have tried both viewDidAppear or ViewWillAppear and passing inside the method but still the picker showing me the first row while it should show me the 5th row – Cem Jan 14 '14 at 16:29
  • Have both the picker and the `res` array been created at the point the code is called? – Austin Jan 14 '14 at 16:33