ios 获取 UIPickerView 当前选中的值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23149781/
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
Get currently selected value of UIPickerView
提问by rbk
I have been googling how to get the currently selected value in UIPickerView, but everywhere it's mentioned using row:0 and so on.
我一直在搜索如何在 UIPickerView 中获取当前选定的值,但在任何地方都提到使用 row:0 等等。
I have a UIPickerView populated with string values like "One","Two", etc.
我有一个 UIPickerView 填充了字符串值,如“一”、“二”等。
Now when I select a value to let's say "Two", is there anyway I can get this text.
现在,当我选择一个值来让我们说“两个”时,无论如何我可以得到这个文本。
Like in UITextView where it is _textview.text
就像在 UITextView 中,它是 _textview.text
回答by Michael Dautermann
Every UIPickerView should have a delegate.
每个 UIPickerView 都应该有一个委托。
And you can ask this delegate for your picker's selected row title via something like:
您可以通过以下方式向此代表询问您的选择器选择的行标题:
UIPickerViewDelegate *delegate = yourPickerView.delegate;
NSString *titleYouWant = [delegate pickerView:yourPickerView titleForRow:[yourPickerView selectedRowInComponent:0] forComponent:0];
(This is ASSUMINGyour component number is zero; your own implementation might be different).
(这是假设您的组件编号为零;您自己的实现可能会有所不同)。
More documentation on the "pickerView:titleForRow:forComponent
" method can be seen here.
回答by Quad
func pickerView(pickerView: UIPickerView!, didSelectRow row: Int, inComponent component: Int)
{
// selected value in Uipickerview in Swift
let value=array[row]
println("values:----------\(value)");
}
回答by Brainstorm Technolabs
First of all add UIPickerViewDatasource and UIPickerViewDelegate in .h file
首先在 .h 文件中添加 UIPickerViewDatasource 和 UIPickerViewDelegate
then in .m add the line
然后在 .m 添加行
self.myPickerView.delegate = self;
now assign array by its objects.
现在通过其对象分配数组。
NSArray *arrOfAge = [[NSArray alloc]initWithObjects:@“one”,@”two”, nil];
This are delegate methods
这是委托方法
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
return 1;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
return [arrOfAge count];
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
NSString *titleRow;
titleRow = [NSString stringWithFormat:@"%@", [arrOfAge objectAtIndex:row]];
return titleRow;
}
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
selectedRow = row;
}
This IBAction btnDone returns the selected row from pickerView.
这个 IBAction btnDone 从 pickerView 返回选定的行。
- (IBAction)btnDone:(id)sender
{
NSUInteger num = [[self.myPickerView dataSource] numberOfComponentsInPickerView:self.myPickerView];
NSMutableString *text = [NSMutableString string];
for(NSUInteger i =0;i<num;++i)
{
NSUInteger selectRow = [self.myPickerView selectedRowInComponent:i];
NSString *ww = [[self.myPickerView delegate] pickerView:self.myPickerView titleForRow:selectRow forComponent:i];
[text appendFormat:@"%@",ww];
NSLog(@"%@",text);
}
}
回答by valfer
If the UIPickerView has only one component (something similar to section for UITableView) you can retrieve the "index" selected with the following line:
如果 UIPickerView 只有一个组件(类似于 UITableView 的部分),您可以使用以下行检索所选的“索引”:
NSInteger indexSelected = [myPicker selectedRowInComponent:0];
(change the parameter 0 if you have more than one component).
(如果您有多个组件,请更改参数 0)。
This is the index in your array of data (myArray), the same you used to return data from the delegate method, as in:
这是数据数组 (myArray) 中的索引,与用于从委托方法返回数据的索引相同,如下所示:
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
return myArray[row];
}
(or similar delegate methods -attributedTitleForRow -viewForRow)
(或类似的委托方法 -attributedTitleForRow -viewForRow)
回答by Sachin Nautiyal
Swift 3
斯威夫特 3
you can use this code for getting current title
您可以使用此代码获取当前标题
self.textview.text = self.pickerView(self.pickerViewOutlet, titleForRow: self.pickerViewOutlet.selectedRow(inComponent: 0), forComponent: 0)