UIPickerView: Is it possible to show the previous selected value in the UIPicker view?

K, Eswara Prabu 41 Reputation points
2023-01-09T08:34:17.153+00:00

For ex: scrolled the pickervirew and selected "2" from the list and on opening the picker for the second time the values in the picker view should show as "2".

In PickerView Delegate: "Selected" delegate function is available to get the selected value. But not able to set the selected value in the picker view

Xamarin
Xamarin
A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
5,296 questions
{count} votes

Accepted answer
  1. Wenyan Zhang (Shanghai Wicresoft Co,.Ltd.) 26,551 Reputation points Microsoft Vendor
    2023-01-10T03:28:04.823+00:00

    Hello,

    You can set the selected value by UIPickerView.Select(nint, nint, Boolean) Method, please refer to the following code: Adding a selected method in the custom UIPickerView

    public void DefultSelect(string selectedYear, string selectedMonth)
            {
                if (this.SelectedYear != null && this.SelectedMonth != null)
                {
                    var yearIndex = yearArray.IndexOf(selectedYear);
                    var monthIndex = monthArray.IndexOf(selectedMonth);
                    PickerViewDelegate pickerDelegate = (PickerViewDelegate)this.Delegate;
                    this.Select(yearIndex, 0, true);
                    this.Select(monthIndex, 1, true);
                }
            }
    

    Calling this method in a ViewController for testing

     clickbutton.TouchUpInside += delegate
                {
                    //get the selected year and month
                    customLabel.Text = customPicker.SelectedYear +"-" + customPicker.SelectedMonth;
                    customPicker.Hidden = true;
                };
    

    Setting the default selected value

    checkSelectbutton.TouchUpInside += delegate
                {
                    customPicker.Hidden = false;
                    string[] words = customLabel.Text.Split('-');
                    string defaultYear = words[0];
                    string defaultMonth = words[1];
                    customPicker.DefultSelect(defaultYear, defaultMonth);// set the value, I get the selected value from the Label, you can store the value (and get the value) according to your needs
                };
    

    Best Regards, Wenyan Zhang


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment". Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.
    0 comments No comments

0 additional answers

Sort by: Most helpful