Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

A simple UIPickerView in MonoTouch (Xamarin)?

Can anybody describe how I can create a UIPickerView in monotouch using XCode and populate it with sample data?

I did look at the example here: https://github.com/xamarin/monotouch-samples/blob/master/MonoCatalog-MonoDevelop/PickerViewController.xib.cs but this hasn't been very helpful since I am creating my UIPickerView in the XCode. Here's what I have so far:

public partial class StatusPickerPopoverView : UIViewController
{
    public StatusPickerPopoverView (IntPtr handle) : base (handle)
    {
        pickerStatus = new UIPickerView();
        pickerStatus.Model = new StatusPickerViewModel();
    }

    public StatusPickerPopoverView (): base ()
    {
    }

    public class StatusPickerViewModel : UIPickerViewModel
    {
        public override int GetComponentCount (UIPickerView picker)
        {
            return 1;
        }

        public override int GetRowsInComponent (UIPickerView picker, int component)
        {
            return 5;
        }

        public override string GetTitle (UIPickerView picker, int row, int component)
        {

            return "Component " + row.ToString();
        }
    }
}
like image 616
P. Sami Avatar asked Mar 22 '13 18:03

P. Sami


4 Answers

So I basically found the answer and it was waaaaaaaaaaaaaaay easier than you can think!

The model has to be set in ViewDidLoad otherwise will crash... That's why it was not being populated correctly. Remember: set it up in "ViewDidLoad".

public partial class StatusPickerPopoverView : UIViewController
{
    public StatusPickerPopoverView (IntPtr handle) : base (handle)
    {
    }

    public override ViewDidLoad()
    {
        base.ViewDidLoad();

        pickerStatus = new UIPickerView();
        pickerStatus.Model = new StatusPickerViewModel();
    }

    public class StatusPickerViewModel : UIPickerViewModel
    {
        public override int GetComponentCount (UIPickerView picker)
        {
            return 1;
        }

        public override int GetRowsInComponent (UIPickerView picker, int component)
        {
            return 5;
        }

        public override string GetTitle (UIPickerView picker, int row, int component)
        {

            return "Component " + row.ToString();
        }
    }
}
like image 66
P. Sami Avatar answered Nov 09 '22 14:11

P. Sami


Xamarin Studio 5.10:

public partial class ViewController : UIViewController
    {
        UIPickerView samplePicker;

        public ViewController (IntPtr handle) : base (handle)
        {
        }    
        public override void ViewDidLoad ()
        {
            base.ViewDidLoad ();

            samplePicker = new UIPickerView (new CoreGraphics.CGRect (10f, 244f, this.View.Frame.Width - 20, 250f));
            samplePicker.BackgroundColor = UIColor.LightGray;    
            samplePicker.Model = new StatusPickerViewModel ();   

        }    
        public class StatusPickerViewModel : UIPickerViewModel
        {
            public override nint GetRowsInComponent (UIPickerView pickerView, nint component)
            {
                return 5;
            }

            public override string GetTitle (UIPickerView pickerView, nint row, nint component)
            {               
                return "Title";
            }

            public override nint GetComponentCount (UIPickerView pickerView)
            {

                return 1;
            }           
        }    
        public override void ViewDidAppear (bool animated)
        {
            base.ViewDidAppear (animated);

            this.View.AddSubview (samplePicker);
        }    
        }
like image 44
Alvin George Avatar answered Nov 09 '22 14:11

Alvin George


I believe you want to set the UIPickerView's Model property with your UIPickerViewModel instance.

pickerStatus.Model = new StatusPickerViewModel();

Here is a Gist that provides a UIPickerViewModel for an IList: https://gist.github.com/jfoshee/5223962

like image 30
Jacob Foshee Avatar answered Nov 09 '22 14:11

Jacob Foshee


I created a blog post, covering it in more detail than the current answers.

Firstly, add a UIPickerView to your Storyboard. Add a name to the UIPicker.

Next, A class implementing UIPickerViewModel is required, which sets the Pickers model which populates it. If you are more familiar with Android development, setting up a Picker for Xamarin iOS can seem weird, you cannot just populate the picker with a list.

Lastly, set the model of the UIPickerView to the class created.

like image 2
cfl Avatar answered Nov 09 '22 15:11

cfl