Ios UIPickerView以编程方式选择第一行
有很多这样的问题,但我相信我的案例是独一无二的 我有一个pickerview,当在里面点击一个文本框时会弹出。用户在pickerview中选择他们的位置,并将其放入文本框中 打开pickerview时,滑块位于第一个元素上,但如果我单击“完成”按钮以最小化pickerview,则不会选择任何元素,即必须向下和向上滚动以选择第一个元素 我试过了Ios UIPickerView以编程方式选择第一行,ios,xcode,uipickerview,Ios,Xcode,Uipickerview,有很多这样的问题,但我相信我的案例是独一无二的 我有一个pickerview,当在里面点击一个文本框时会弹出。用户在pickerview中选择他们的位置,并将其放入文本框中 打开pickerview时,滑块位于第一个元素上,但如果我单击“完成”按钮以最小化pickerview,则不会选择任何元素,即必须向下和向上滚动以选择第一个元素 我试过了 [pickerView selectRow:0 inComponent:0 animated:YES]; 和它的各种组合,但它只将滑块放在该元素
[pickerView selectRow:0 inComponent:0 animated:YES];
和它的各种组合,但它只将滑块放在该元素上,而不是实际选择它
一些答案说在viewDidLoad方法中应用了上述代码,但不幸的是,我正在从web服务中提取位置数组,无法做到这一点
理想情况下,我想做这样的工作——用户在文本框中单击,pickerview会正常弹出,如果此时单击“完成”按钮,则会选择第一项
提前谢谢以下是您可以尝试的: 在@interface中设置一个var,类似于NSString*selectedString 在pickerView:titleForRow:forComponent:中初始化UIPickerView时,设置行0的标题时,请使用与行0标题相同的字符串设置selectedString。然后,在pickerView:didSelectRow:Incomonent:中,使用适当的字符串设置selectedString:
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
// Some stuff
// Assuming "yourDataArray" contains your strings
selectedString = [yourDataArray objectAtIndex:row];
}
然后,当您调用由按下“完成”按钮触发的方法时,使用selectedString设置文本
希望这对你有用 /为textfield创建一个iAction,事件编辑已开始,函数应检查字段是否为空,如下所示/
我也有同样的问题。我的解决办法是:
-(void)textFieldDidBeginEditing:(UITextField *)textField
{
// Only for the text field with the picker
if (textField == self.yourTextField)
{
// Select the first row programmatically
[self.categoriesPicker selectRow:0 inComponent:0 animated:YES];
// The delegate method isn't called if the row is selected programmatically
[self pickerView:self.categoriesPicker didSelectRow:0 inComponent:0];
}
}
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
//Do all the stuff to populate the TextField
}
斯威夫特3+
如果textfield不是空的,还需要设置一个条件。我们不必忘记textfield的委托。yourTextField.delegate=self
-(void)textFieldDidBeginEditing:(UITextField *)textField
{
// Only for the text field with the picker
if (textField == self.yourTextField)
{
// Select the first row programmatically
[self.categoriesPicker selectRow:0 inComponent:0 animated:YES];
// The delegate method isn't called if the row is selected programmatically
[self pickerView:self.categoriesPicker didSelectRow:0 inComponent:0];
}
}
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
//Do all the stuff to populate the TextField
}
-(void)textFieldDidBeginEditing:(UITextField *)textField
{
// Only for the text field with the picker
if (textField == self.yourTextField && [textField length]!=0)
{
// Select the first row programmatically
[self.categoriesPicker selectRow:0 inComponent:0 animated:YES];
// The delegate method isn't called if the row is selected programmatically
[self pickerView:self.categoriesPicker didSelectRow:0 inComponent:0];
}
}
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
//Do all the stuff to populate the TextField
}
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
}