xcode UIPickerView 以编程方式选择第一行
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14605222/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
UIPickerView select first row programmatically
提问by Jonesopolis
There are a lot of questions like this, but I believe my case is unique.
有很多这样的问题,但我相信我的情况是独一无二的。
I have a pickerview that pops up when a textbox is clicked inside. The user selects their location in the pickerview, and it's put into the textbox.
我有一个选择器视图,当在里面单击文本框时会弹出它。用户在选择器视图中选择他们的位置,并将其放入文本框中。
When opening the pickerview, the slider is on the first element, but if i click the done button to minimize the pickerview, that no element is selected (i.e. you must scroll down and back up to select the first element)
打开pickerview时,滑块位于第一个元素上,但如果我单击完成按钮以最小化pickerview,则未选择任何元素(即您必须向下滚动并向上滚动以选择第一个元素)
I've tried
我试过了
[pickerView selectRow:0 inComponent:0 animated:YES];
and various combinations of it, but it only puts the slider on that element, again not actually selecting it.
以及它的各种组合,但它只会将滑块放在该元素上,实际上并没有选择它。
Some answers said to apply the above code in the viewDidLoad() method, but unfortunately I'm pulling the location array in from a web service and cannot do that.
一些答案说要在 viewDidLoad() 方法中应用上述代码,但不幸的是,我正在从 Web 服务中提取位置数组,但不能这样做。
Ideally, I'd like to work as - the user clicks in the textbox, the pickerview pops up as normal, if at that point the done button is clicked, the first item is selected.
理想情况下,我想工作 - 用户在文本框中单击,选择器视图正常弹出,如果此时单击完成按钮,则选择第一个项目。
Thankyou in advance
先感谢您
采纳答案by rdurand
Here is something you can try :
您可以尝试以下方法:
Set a var in your @interface
, something like NSString *selectedString;
.
在您的 中设置一个 var @interface
,类似于NSString *selectedString;
.
When you init your UIPickerView
(in pickerView:titleForRow:forComponent:
), when you set the title for the row 0, set selectedString
with the same string as the title of the row #0. Then, in pickerView:didSelectRow:inComponent:
, set selectedString
with the appropiate string :
当您初始化您的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];
}
Then, when you call the method triggered by the push of the "done" button, use selectedString
to set the text.
然后,当您调用由按下“完成”按钮触发的方法时,使用selectedString
来设置文本。
Hope this works out for you !
希望这对你有用!
回答by Dasoga
Swift 3+
斯威夫特 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
}
回答by gUIDo
I had a same problem. My solution was this:
我有同样的问题。我的解决方案是这样的:
-(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
}
回答by nithinreddy
-(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
}
回答by Andres Ortiz
/Create a IBACTION for the textfield with event editing did begin and the function should check if field is empty like this/
/为带有事件编辑的文本字段创建一个 IBACTION 确实开始了,该函数应该像这样检查字段是否为空/
-(IBAction)asignFirst:(UITextField *)sender{
if ([sender.text isEqualToString:@""])
{
[yourPicker selectRow:0 inComponent:0 animated:YES];
sender.text = self.pickerData[0];
}
}