سؤال

I have created a UIPicker, which has about eight or nine possible strings. Depending on what view loads the UIPicker will depend on which strings are shown.

Due to this in my pickerView:didSelectRow:inComponent so I was wondering if there is a way when the user selects a row if I can get the textvalue that is in that.

This way in the didselectrow method I can perform different actions depending on the string value that was selected.

Current this is what I have:

#pragma mark - Picker Delegates
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {

// get rowString somehow?
    // ReloadView
    if ([rowString isEqualToString:@"one Of the Values"]) {

     }
    // more if statements here.
}
هل كانت مفيدة؟

المحلول

Just read the value from the data source for that row then compare:

NSString *rowString = (NSString *)[self.pickerArray objectAtIndex:row];
if ([rowString isEqualToString:@"one Of the Values"]) {

 }

نصائح أخرى

@Meda gave you the right answer. You should not store data in a view object like a picker. Remember, in order for a UIPickerView to work, you have to implement the method pickerView:titleForRow:forComponent:' (or the methodpickerView:viewForRow:forComponent:reusingView:' if you build your cell views yourself). The `pickerView:titleForRow:forComponent:' method provides the text for each row in a component of your picker view, so it has to have the data available to it.

Therefore, the delegate of the UIPickerView needs to have that data available to it. Meda's post assumed you saved it in an array pickerArray.

[pickerView selectedRowInComponent:0];
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top