很抱歉,因为这个问题之前已经问过,但我就是无法理解它。
我有一个 UIPickerView 可以正常工作,我用
将它添加到我的 UIViewController _pv = [[CategoryPicker alloc] init];
_textField.inputView = _pv;
_pv 是 UIPickerView,_textField 是我希望显示结果的文本字段。
所以当我使用这行代码时:
_pv.categoryPicker.delegate = self; //set in my viewDidLoad
然后将其放置在 UIViewController 中:
- (void)pickerView:(UIPickerView *)pickerView didSelectRow: (NSInteger)row inComponent:(NSInteger)component {
// Handle the selection
_textField.text = [_pv.categoryArray objectAtIndex:row];
}
我能够填充 TextField,但 UIPickerView 充满了“?”对于数组的每个条目。
现在当我这样做时:
_pv.categoryPicker.delegate = _pv.self; //set in my viewDidLoad
然后我把它放在 UIPickerView 中:
- (void)pickerView:(UIPickerView *)pickerView didSelectRow: (NSInteger)row inComponent:(NSInteger)component {
// Handle the selection
_outputArray = [_categoryArray objectAtIndex:row];
NSLog(@"The item selected = %@", _outputArray);
}
UIPickerView 已填充,但数据未传递到 TextField。
所以这就是我卡住的地方 - 我需要在两个地方都有结果。 现在我想我用子类来做到这一点,但我读过的所有内容都不适合我。
请帮帮我:-)
提前致谢。
最佳答案
您应该查看 UIPickerViewDelegate 的完整文档协议(protocol)。
基本上,您要做的是将 UIViewController 设置为 UIPickerView 的委托(delegate),如下所示:
_pv.categoryPicker.delegate = self;
然后在您的 UIViewController 中您要实现以下 UIPickerViewDelegate 方法:
- (void)pickerView:(UIPickerView *)pickerView didSelectRow: (NSInteger)row inComponent:(NSInteger)component {
// called when a row is selected
_textField.text = [_pv.categoryArray objectAtIndex:row];
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
// here is where you populate the contents of the pickerView
// return the title that you want to appear in 'row'
return [NSString stringWithFormat:@"row %d", row];
}
关于objective-c - ios - 在子类化时从 UIPickerView 传递值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11215923/