c# - 为什么我的 UIPickerView 在选择时崩溃

标签 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;
    }
}

我不知道为什么它不断崩溃,是我在模型中缺少某种方法还是我试图以错误的方式执行此操作?

谢谢

最佳答案

尝试将您的 catPicker 设为该类的私有(private)变量:

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

看起来 GC 已经收集了你的 catPicker 并处理了它。

关于c# - 为什么我的 UIPickerView 在选择时崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10744267/

相关文章:

c# - FormClosing 需要新线程吗?

xamarin.ios - 在运行时更改 UIBarButton 标识符

ios - Swift 中的 UIPickerView 子类 : error optional value in viewForRow

ios - 点击字段时,数据未填充到 UIPickerView 中

c# - 有没有办法阻止 Visual Studio 将线程退出语句打印到输出窗口中?

c# - 使用设置的前缀创建 GUID

c# - 为 System.NotImplementedException/System.NotSupportedException 编写单元测试是一种好习惯吗

xamarin.ios - 在 MonoTouch 中如何映射派生自两个类的接口(interface)?

c# - 添加 UITabBarController 时出错

iphone - 单击按钮时隐藏显示 UIPicker