Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Xamarin iOS UiPickerView tutorial

I'm trying to find just some basic instructions on using UiPickerView in Xamarin iOS. There are questions here on Stack Overflow that answer certain questions, but I'm looking for just a simple overview and hopefully walk-through of adding the UiPickerView to a ViewController, wiring up the data, and registering events. As a noob to Xamarin and iOS, I'm struggling to catalogue all the places the code for this type of control need to go.

like image 290
Andrew B Schultz Avatar asked Apr 08 '16 22:04

Andrew B Schultz


3 Answers

// Set PickerViewModel to PickerView

var examplePVM = new ExamplePickerViewModel(myListOfItems);
YourPickerView.Model = examplePVM ;

// Create a PickerViewModel

public class ExamplePickerViewModel : UIPickerViewModel
{
    private List<string> _myItems;
    protected int selectedIndex = 0;

    public ExamplePickerViewModel(List<string> items)
    {
        _myItems = items;
    }

    public string SelectedItem
    {
        get { return _myItems[selectedIndex]; }
    }

    public override nint GetComponentCount (UIPickerView picker)
    {
        return 1;
    }

    public override nint GetRowsInComponent (UIPickerView picker, nint component)
    {
        return _myItems.Count;
    }

    public override string GetTitle (UIPickerView picker, nint row, nint component)
    {
        return _myItems[row];
    }

    public override void Selected (UIPickerView picker, nint row, nint component)
    {
        selectedIndex = (int)row; 
    }
}
like image 148
JustFrago Avatar answered Nov 19 '22 00:11

JustFrago


// Textfield Click event
private bool OnTodeapartmentShouldBeginEditing(UITextField textField)
{

// Creating Picker view 
pickerView = new UIPickerView(new CGRect(UIScreen.MainScreen.Bounds.X-UIScreen.MainScreen.Bounds.Width, UIScreen.MainScreen.Bounds.Height -230, UIScreen.MainScreen.Bounds.Width, 180));
pickerView.BackgroundColor = UIColor.From#d3d3d3;
pickerView.ShowSelectionIndicator = true;

// create done button
done = new UIButton(new CGRect(pickerView.Frame.X, pickerView.Frame.Y - 50, UIScreen.MainScreen.Bounds.Width, 50));
done.BackgroundColor = UIColor.Purple;
done.SetTitle("Department List", UIControlState.Normal);

picker = new PickerDataModel(); 
if (departmentList.DepartmentDetail != null)
{

foreach (var item in departmentList.DepartmentDetail)
{
picker.Items.Add(item.Name);
}
pickerView.Model = picker;

view.AddSubviews(pickerView, done);
// value change event of picker view
picker.ValueChanged += (s, e) =>
{
selectedValue = picker.SelectedItem;
txtDepartment.Text = selectedValue;

deptID = departmentList.DepartmentDetail.Find(x => x.Name == selectedValue).DepartmentId;

}; 
}
}

New Class File enter image description here // Picker Data Model Class for picker view , this class file is using on helper section for common use

public class PickerDataModel : UIPickerViewModel
{
public event EventHandler<EventArgs> ValueChanged;

/// <summary>
/// The items to show up in the picker
/// </summary>
public List<string> Items { get; private set; }

/// <summary>
/// The current selected item
/// </summary>
public string SelectedItem
{
get { return Items[SelectedIndex]; }
}

public int SelectedIndex
{
get
{
return selectedIndex;
}

set
{
selectedIndex = value;
}
}

private int selectedIndex;

public PickerDataModel()
{
Items = new List<string>();
}

/// <summary>
/// Called by the picker to determine how many rows are in a given spinner item
/// </summary>
public override nint GetRowsInComponent(UIPickerView picker, nint component)
{
return Items.Count;
}

/// <summary>
/// called by the picker to get the text for a particular row in a particular
/// spinner item
/// </summary>
public override string GetTitle(UIPickerView picker, nint row, nint component)
{
return Items[(int)row];
}

/// <summary>
/// called by the picker to get the number of spinner items
/// </summary>
public override nint GetComponentCount(UIPickerView picker)
{
return 1;
}

/// <summary>
/// called when a row is selected in the spinner
/// </summary>
public override void Selected(UIPickerView picker, nint row, nint component)
{
SelectedIndex = (int)row;
if (ValueChanged != null)
{
ValueChanged(this, new EventArgs());
}

}
} [enter image description here][1]


  [1]: https://i.stack.imgur.com/Ld1uU.png
like image 2
sudeep chourasia Avatar answered Nov 19 '22 00:11

sudeep chourasia


This PickerViewController file in the MonoCatalog-MonoDevelop sample sounds like what you're asking for.

In particular, the CreateCustomPicker function here and the CustomPickerModel type defined here.

like image 1
Larry OBrien Avatar answered Nov 18 '22 23:11

Larry OBrien