Reputation: 2413
I have three labels, which I want to fill with selected row from pickerview. Like if 1st label is already filled then the second should be filled with the selected row value and if 1st and second both are already filled then the third should be filled.
I am trying this code but it is assigning value only to the first label.
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row
inComponent:(NSInteger)component
{
NSString *selectedReward = [self.rewards objectAtIndex:row];
if ([reward1.text isEqualToString:selectedReward]) {
if ([reward2.text isEqualToString:selectedReward]) {
reward3.text = selectedReward;
}
reward2.text = selectedReward;
}
else {
reward1.text = selectedReward;
}
}
Upvotes: 0
Views: 1640
Reputation: 12333
try check UILabel
by comparing its length
.
if ([label1.text length]==0){
//set label 1
}
else if ([label2.text length]==0){
//set label 2
}
else{
//set label 3.
}
Upvotes: 0
Reputation: 8564
In your code you are checking label's text with selectedReward
.
So, if you select each time different picker component then it will just updated the first label(will enter in else part of your code), but if you select same picker component(as you selected for first label) then it will enter in if part.
You can proceed with what @loser1089906
suggested.
Upvotes: 1
Reputation: 46
I think it's because you always select the different row, and the row text is different. Then the selected value is always different with the first lable's text, so the function will set the label1's text.
I think you should test the text length.
if ([reward1.text length] == 0) {reward1.text = selectedReward;} // and so on
Upvotes: 1