Jon*_*lis 13 xcode uipickerview ios
有很多这样的问题,但我相信我的案例是独一无二的.
我在内部单击文本框时会弹出一个选择器视图.用户在选择器视图中选择他们的位置,并将其放入文本框中.
打开选择器视图时,滑块位于第一个元素上,但是如果我单击完成按钮以最小化选择器视图,则不会选择任何元素(即,您必须向下滚动并向上选择第一个元素)
我试过了
[pickerView selectRow:0 inComponent:0 animated:YES];
Run Code Online (Sandbox Code Playgroud)
以及它的各种组合,但它只将滑块放在该元素上,而不是实际选择它.
一些答案说要在viewDidLoad()方法中应用上面的代码,但不幸的是我从Web服务中提取位置数组并且不能这样做.
理想情况下,我想工作 - 用户在文本框中单击,选择器视图正常弹出,如果在此时单击完成按钮,则选择第一个项目.
先感谢您
Das*_*oga 12
Swift 3+
func textFieldDidBeginEditing(_ textField: UITextField) {
if textField == yourTextField{
self.yourPickerView.selectRow(0, inComponent: 0, animated: true)
self.pickerView(yourPickerView, didSelectRow: 0, inComponent: 0)
}
}
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
//Do all the stuff to populate the TextField
}
Run Code Online (Sandbox Code Playgroud)
您可以尝试以下方法:
设置你的var @interface,就像NSString *selectedString;.
当您初始化UIPickerView(in pickerView:titleForRow:forComponent:)时,当您为行0设置selectedString标题时,使用与行#0的标题相同的字符串进行设置.然后,在pickerView:didSelectRow:inComponent:,selectedString使用适当的字符串设置:
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
// Some stuff
// Assuming "yourDataArray" contains your strings
selectedString = [yourDataArray objectAtIndex:row];
}
Run Code Online (Sandbox Code Playgroud)
然后,当您调用通过按下"完成"按钮触发的方法时,使用selectedString设置文本.
希望这对你有用!
| 归档时间: |
|
| 查看次数: |
16597 次 |
| 最近记录: |