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

本文关键字:实例 iOS 识别 选择器 | 更新日期: 2023-09-27 18:13:18

我试图显示一个从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 ();}}

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

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

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