简体   繁体   中英

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

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

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

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM