user1607001
user1607001

Reputation: 19

Change label on select. UIPickverView

The code I'm using right now isn't working, every time I load it and scroll in the UIPickerView it automatically uses the value of the "else" statement.

- (void)pickerView:(UIPickerView *)thePickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
if ([pickerView selectedRowInComponent:0]) {
    [label setText:@"row1"];
}
if ([pickerView selectedRowInComponent:3]) {
    [label setText:@"row2"];
}
if ([pickerView selectedRowInComponent:4]) {
    [label setText:@"row3"];
}
else {
     [label setText:@"row else"];
}
}

What am I doing wrong?

Upvotes: 0

Views: 200

Answers (5)

Ankit Thakur
Ankit Thakur

Reputation: 4749

try this:

since you are using numberOfComponentsInPickerView = 1 in return type.

then, why are you comparing with rowInComponent api.

- (void)pickerView:(UIPickerView *)thePickerView didSelectRow:(NSInteger)row inComponent: (NSInteger)component {

   if(row == 0)
     [label setText:@"row3"];

   else if(row == 1)
     [label setText:@"row3"];

   else if(row == 3)
     [label setText:@"row3"];

   else
     [label setText:@"other"]; // this line will be called when row will be 2 or >3
  }

How many rows are there in – pickerView:numberOfRowsInComponent: method.

Upvotes: 1

CBredlow
CBredlow

Reputation: 2840

The reason is it doesn't meet any of these conditions, selectedRowInComponent returns an NSInteger, not a boolean. So you would have to update the if conditionals to include a value, such as

 [selectedRowInComponent:1]==1

Upvotes: 0

gdavis
gdavis

Reputation: 2585

You're basing all of your conditions on the return value of this method: selectedRowInComponent which returns an integer value. Because its returning an index for the selected row in that component, your value could be from -1 to X, X being how many rows you have in a component.

If you want your text to update when anything is selected on one of your rows, you need to see if your selected row index is greater than -1, which would mean a row was actually selected in your picker view.

So try something more like this:

if ([pickerView selectedRowInComponent:0] > -1) {
    [label setText:@"row1"];
}
else if ([pickerView selectedRowInComponent:3] > -1) {
    [label setText:@"row2"];
}
else if ([pickerView selectedRowInComponent:4] > -1) {
    [label setText:@"row3"];
}
else {
     [label setText:@"row else"];
}

Upvotes: 0

Sumanth
Sumanth

Reputation: 4921

Use the code similar to this.

- (void)pickerView:(UIPickerView *)thePickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {

{
if (pickerView.tag==1)
  {
    if (row == 0)
    {
          [label setText:@"row1"];
    }

   else if (row == 1)
    {
         [label setText:@"row2"];

    }

   else if (row == 2)
    {
            [label setText:@"row3"];

    }
   else{
            [label setText:@"row else"];

       }
  }
}

Upvotes: 0

Rehat Kathuria
Rehat Kathuria

Reputation: 195

Instead of creating many if statements, try making the if statements into "else if" statements.

- (void)pickerView:(UIPickerView *)thePickerView didSelectRow:(NSInteger)row inComponent:    (NSInteger)component {
if ([pickerView selectedRowInComponent:0]) {
    [label setText:@"row1"];
}
else if ([pickerView selectedRowInComponent:3]) {
    [label setText:@"row2"];
}
else if ([pickerView selectedRowInComponent:4]) {
    [label setText:@"row3"];
} 
else {
     [label setText:@"row else"];
}
}

Upvotes: 0

Related Questions