为什么我的UIPickerView在选择时会崩溃

时间:2012-05-24 19:45:52

标签: c# xamarin.ios uipickerview uiactionsheet

我在github Github link

上创建了一个示例

当点击一行或试图在UIPickerView中滚动它会崩溃,我想知道为什么。

// create a ActionSheet
var actionPickerSheet = new UIActionSheet("Select a Category");
actionPickerSheet.Style = UIActionSheetStyle.BlackTranslucent;
// Create UIpickerView
var catPicker = new UIPickerView(){
    Model = new catPickerModel(),
    AutosizesSubviews = true,
    Hidden = false,
    ShowSelectionIndicator = true
};
// show inside view and add the catPicker as subView
actionPickerSheet.ShowInView(View);
actionPickerSheet.AddSubview(catPicker);
// resize both views so it fits smoothly
actionPickerSheet.Frame = new RectangleF(0,100,320,500);
catPicker.Frame = new RectangleF(actionPickerSheet.Frame.X,actionPickerSheet.Frame.Y-25,actionPickerSheet.Frame.Width, 216);

和模型

private class catPickerModel : UIPickerViewModel
{
    public string[] protocolNames = new string[]
    {
        "Web", "Phone Call", "Google Maps", "SMS", "Email"
    };

    public override int GetComponentCount(UIPickerView uipv)
    {
        return 1;
    }

    public override int GetRowsInComponent( UIPickerView uipv, int comp)
    {
        //each component has its own count.
        int rows = protocolNames.Length;
        return(rows);
    }

    public override string GetTitle(UIPickerView uipv, int row, int comp)
    {  
        //each component would get its own title.
        return protocolNames[row];
    }

    public override void Selected(UIPickerView uipv, int row, int comp)
    {
        Console.WriteLine("selected:" + row);
    }

    public override float GetComponentWidth(UIPickerView uipv, int comp)
    {
        return 300f;
    }
}

我不知道它为什么会一直崩溃,这是我在模型中缺少的一些方法,还是我试图以错误的方式做到这一点?

由于

1 个答案:

答案 0 :(得分:3)

尝试将catPicker设为班级的私有变量:

namespace TextfieldUIPickerView
{
    public partial class TextfieldUIPickerViewViewController : UIViewController
    {
        private UIPickerView catPicker;
 ...

看起来GC收集了你的catPicker并将其丢弃。