Reputation:
I know that with a UIDatePicker, you can use something like:
NSDate *myDate = picker.date;
But I am using a UIPickerView in my view. How can i similarly get the value selected? Or do I have to setup didSelectRow type of method to do this?
Update: This code works for picker with 1 component:
NSInteger row;
NSString *weightSelected;
row = [repPicker selectedRowInComponent:0];
weightSelected = [pickerArray objectAtIndex:row];
I tired this code for my picker with 2 components, but it is freezing:
NSInteger row1, row2;
NSString *weightSelected1;
NSString *weightSelected2;
row1 = [repPicker selectedRowInComponent:0];
row2 = [repPicker selectedRowInComponent:1];
weightSelected1 = [pickerArray objectAtIndex:row1];
weightSelected2 = [pickerArray objectAtIndex:row2];
NSString *weightSelected = [NSString stringWithFormat:@"%@.%@", weightSelected1, weightSelected2];
Upvotes: 80
Views: 123665
Reputation: 1
NSInteger SelectedRow;
SelectedRow = [yourPickerView selectedRowInComponent:0];
selectedPickerString = [YourPickerArray objectAtIndex:SelectedRow];
self.YourTextField.text= selectedPickerString;
// if you want to move pickerview to selected row then
for (int i = 0; I<YourPickerArray.count; i++) {
if ([[YourPickerArray objectAtIndex:i] isEqualToString:self.YourTextField.text]) {
[yourPickerView selectRow:i inComponent:0 animated:NO];
}
}
Upvotes: 0
Reputation: 34983
Getting the selected title of a picker:
let component = 0
let row = picker.selectedRow(inComponent: component)
let title = picker.delegate?.pickerView?(picker, titleForRow: row, forComponent: component)
Upvotes: 0
Reputation: 5143
You will need to ask the picker's delegate, in the same way your application does. Here is how I do it from within my UIPickerViewDelegate:
func selectedRowValue(picker : UIPickerView, ic : Int) -> String {
//Row Index
let ir = picker.selectedRow(inComponent: ic);
//Value
let val = self.pickerView(picker,
titleForRow: ir,
forComponent: ic);
return val!;
}
Upvotes: 3
Reputation: 1
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
weightSelected = [pickerArray objectAtIndex:row];
}
Upvotes: -2
Reputation: 2645
You can get it in the following manner:
NSInteger row;
NSArray *repeatPickerData;
UIPickerView *repeatPickerView;
row = [repeatPickerView selectedRowInComponent:0];
self.strPrintRepeat = [repeatPickerData objectAtIndex:row];
Upvotes: 132
Reputation: 1
This is my answer
- (IBAction)Result:(id)sender
{
self.statusLabel.text = DataSource[[pickerViewTool selectedRowInComponent:0]];
}
Upvotes: -5
Reputation: 94683
You can access the selected row for a given component using the following method:
- (NSInteger)selectedRowInComponent:(NSInteger)component
Otherwise, implementing the delegate function is the only other option.
Upvotes: 6
Reputation: 8065
This is what I did:
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
selectedEntry = [allEntries objectAtIndex:row];
}
The selectedEntry
is a NSString
and will hold the currently selected entry in the pickerview. I am new to objective C but I think this is much easier.
Upvotes: 7
Reputation: 1482
You can get the text of the selected item in any section of the picker using the same function that the pickerView does, from your custom ViewController class:
-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
using the selected item
[myPickerView selectedRowInComponent:0]
so to set the text of a label in a custom cell using the currently selected value from the 2nd section of myPickerView (a property of your view controller probably)
[cell.label setText:[self pickerView:myPickerView titleForRow:[myPickerView selectedRowInComponent:1] forComponent:1]];
Just change both :1s to :2s, etc for each section
Upvotes: 34
Reputation: 243146
You have to use the didSelectRow
delegate method, because a UIPickerView
can have an arbitrary number of components. There is no "objectValue" or anything like that, because that's entirely up to you.
Upvotes: 0