我已经创建了一个UIPicker,它大约有八到九个可能的字符串。根据加载的视图,UIPicker将取决于所显示的字符串。
由于我的pickerView:didSelectRow:inComponent中有这一点,所以我想知道当用户选择一行时,是否有一种方法可以让我获得其中的文本值。
这样,在didselectrow方法中,我可以根据选择的字符串值执行不同的操作。
目前我所拥有的是:
#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.
}
发布于 2014-04-04 04:42:43
只需从数据源中读取该行的值,然后进行比较:
NSString *rowString = (NSString *)[self.pickerArray objectAtIndex:row];
if ([rowString isEqualToString:@"one Of the Values"]) {
}
发布于 2014-04-04 21:23:59
@Meda给了你正确的答案。您不应该像选择器一样将数据存储在视图对象中。请记住,为了让UIPickerView工作,如果您自己构建单元格视图,则必须实现方法pickerView:titleForRow:forComponent:' (or the method
pickerView:viewForRow:forComponent:reusingView:‘)。` `pickerView:titleForRow:forComponent:‘方法为选择器视图的组件中的每一行提供文本,因此它必须具有可用的数据。
因此,UIPickerView的委托需要使该数据可供其使用。Meda的帖子假设你将其保存在数组pickerArray
中。
发布于 2014-04-04 04:37:22
[pickerView selectedRowInComponent:0];
https://stackoverflow.com/questions/22848393
复制相似问题