How to get selected value from UIPickerView

I know that with UIDatePicker you can use something like:

NSDate *myDate = picker.date; 

But I use UIPickerView in my opinion. How can I choose a value in the same way? Or do I need to configure the didSelectRow method for this?

Update: This code works for a collector with 1 component:

  NSInteger row; NSString *weightSelected; row = [repPicker selectedRowInComponent:0]; weightSelected = [pickerArray objectAtIndex:row]; 

I am tired of this code for my assembler with two components, but it freezes:

  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]; 
+64
ios objective-c cocoa-touch
Apr 20
source share
7 answers

You can get it as follows:

 NSInteger row; NSArray *repeatPickerData; UIPickerView *repeatPickerView; row = [repeatPickerView selectedRowInComponent:0]; self.strPrintRepeat = [repeatPickerData objectAtIndex:row]; 
+118
Apr 20 2018-11-11T00:
source share

You can get the text of the selected item in any section of the collector using the same function as pickerView from your custom ViewController class:

 -(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component 

using the selected item

 [myPickerView selectedRowInComponent:0] 

to set label text in a custom cell using the currently selected value from the 2nd section of myPickerView (possibly a property of your view controller)

 [cell.label setText:[self pickerView:myPickerView titleForRow:[myPickerView selectedRowInComponent:1] forComponent:1]]; 

Just change both: 1s to: 2s, etc. for each section

+30
Mar 30 2018-12-12T00:
source share

You can access the selected row for this component using the following method:

 - (NSInteger)selectedRowInComponent:(NSInteger)component 

Otherwise, the implementation of the delegate function is the only other option.

+5
Apr 20 '11 at 4:45
source share

This is what I did:

 - (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component { selectedEntry = [allEntries objectAtIndex:row]; } 

selectedEntry is an NSString and will hold the currently selected record in the pickerview element. I am new to C object, but I think it is a lot easier.

+5
Jun 21 '13 at 12:09 on
source share

You must use the didSelectRow delegate didSelectRow , because a UIPickerView can have an arbitrary number of components. There is no "objectValue" or anything like that, because it is entirely up to you.

+1
Apr 20 '11 at 4:45
source share
 -(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component { weightSelected = [pickerArray objectAtIndex:row]; } 
-2
Feb 05 '16 at 10:17
source share

This is my answer

 - (IBAction)Result:(id)sender { self.statusLabel.text = DataSource[[pickerViewTool selectedRowInComponent:0]]; } 
-5
Oct 14 '14 at 19:27
source share



All Articles