15

谁能描述我如何使用 XCode 在 monotouch 中创建 UIPickerView 并用示例数据填充它?

我确实在这里查看了示例:https ://github.com/xamarin/monotouch-samples/blob/master/MonoCatalog-MonoDevelop/PickerViewController.xib.cs但这并不是很有帮助,因为我正在创建我的 UIPickerView X代码。这是我到目前为止所拥有的:

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

4 回答 4

21

所以我基本上找到了答案,它比你想象的更容易!

模型必须在 ViewDidLoad 中设置,否则会崩溃......这就是它没有被正确填充的原因。请记住:在“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();
        }
    }
}
于 2013-03-25T15:21:23.137 回答
3

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);
        }    
        }
于 2016-03-17T06:55:59.287 回答
2

我相信你想用你的 UIPickerViewModel 实例设置 UIPickerView 的 Model 属性。

pickerStatus.Model = new StatusPickerViewModel();

这是一个为 IList 提供 UIPickerViewModel 的要点:https ://gist.github.com/jfoshee/5223962

于 2013-03-22T19:16:54.127 回答
2

我创建了一篇博文,比当前的答案更详细地介绍了它。

首先,将 UIPickerView 添加到您的 Storyboard 中。为 UIPicker 添加名称。

接下来,需要一个实现 UIPickerViewModel 的类,它设置填充它的 Pickers 模型。如果您更熟悉 Android 开发,为 Xamarin iOS 设置选取器可能看起来很奇怪,您不能只用列表填充选取器。

最后,将 UIPickerView 的模型设置为创建的类。

于 2017-06-08T18:43:07.943 回答