无法识别的选择器发送到实例c#iOS

时间:2016-10-14 18:52:58

标签: c# ios xamarin uipickerview

我试图显示从viewdidload方法调用的选择器视图。它编译得很好但是在显示它的那一刻会引发错误。我添加了一个名为“colorPicker”的选择器和一个名为“colorValueLabel”的标签,该标签应该在选择器中显示所选的选项。感谢

错误:

Foundation.MonoTouchException: Objective-C exception thrown.  Name: NSInvalidArgumentException Reason: -[PickerDataModel initWithCoder:]: unrecognized selector sent to instance 0x1888a9e0

这是我的Picker类

 partial 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]; }
    }

    int selectedIndex = 0;

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

这是viewdidload方法。

public partial class DatosPersonalesController : UIViewController
{
    PickerDataModel pickerDataModel;
    ControladorDatosPersonales datosPersonalesController;
    SideBarController sidebar;
    string email;
    string celular;
    string idintranet;
    string tipo;
    string idpersona;
    string os;
    string appversion;

    NoInternetConnectionView noInternetView;

    public DatosPersonalesController (IntPtr handle) : base (handle)
    {
    }

    public override void ViewDidLoad ()
    {
        //inicio picker
        // Perform any additional setup after loading the view, typically from a nib.
        // create our simple picker model
        pickerDataModel = new PickerDataModel();
        pickerDataModel.Items.Add ("Blue");
        pickerDataModel.Items.Add ("Red");
        pickerDataModel.Items.Add ("Purple");
        pickerDataModel.Items.Add ("White");

        // set it on our picker class
        //colorPicker = pickerDataModel;

        // wire up the value change method
        pickerDataModel.ValueChanged += (s, e) => {
            colorValueLabel.Text = pickerDataModel.SelectedItem;
        };

        // set our initial selection on the label
        colorValueLabel.Text = pickerDataModel.SelectedItem;
        //fin picker 
        base.ViewDidLoad ();}}

1 个答案:

答案 0 :(得分:0)

添加以下缺少的构造函数以允许从unarchiver(即.Storyboard.nib)初始化类:

[Foundation.Export("initWithCoder:"]
public PickerDataModel(Foundation.NSCoder coder) { }