Domanda

I have been googling how to get the currently selected value in UIPickerView, but everywhere it's mentioned using row:0 and so on.

I have a UIPickerView populated with string values like "One","Two", etc.

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

È stato utile?

Soluzione

Every UIPickerView should have a delegate.

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 ASSUMING your component number is zero; your own implementation might be different).

More documentation on the "pickerView:titleForRow:forComponent" method can be seen here.

Altri suggerimenti

func pickerView(pickerView: UIPickerView!, didSelectRow row: Int, inComponent component: Int)
{
    // selected value in Uipickerview in Swift
    let value=array[row]
    println("values:----------\(value)");

}

First of all add UIPickerViewDatasource and UIPickerViewDelegate in .h file

then in .m add the line

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:(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);
    }
}

If the UIPickerView has only one component (something similar to section for UITableView) you can retrieve the "index" selected with the following line:

NSInteger indexSelected = [myPicker selectedRowInComponent:0];

(change the parameter 0 if you have more than one component).

This is the index in your array of data (myArray), the same you used to return data from the delegate method, as in:

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
 return myArray[row];
}

(or similar delegate methods -attributedTitleForRow -viewForRow)

Swift 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)

If you use an array to initialize the picker you can use:

let p0 = pickerArray[0][pickerViewOutlet.selectedRow(inComponent: 0)]
let p1 = pickerArray[1][pickerViewOutlet.selectedRow(inComponent: 1)]
let p2 = pickerArray[2][pickerViewOutlet.selectedRow(inComponent: 2)]

etc.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top