PickerView ToolBar的完成按钮不起作用

时间:2014-09-26 18:20:49

标签: c# ios xamarin uipickerview toolbar

将完成按钮添加到pickerview工具栏, 但是点击完成按钮点击事件不起作用。

    public override void ViewDidLoad(){
         myPickerView = new UIPickerView (RectangleF.Empty){
            AutoresizingMask = UIViewAutoresizing.FlexibleWidth,
            ShowSelectionIndicator = true,
            Model = model,
            Hidden = true,
            BackgroundColor = UIColor.Clear
        };

        toolbar = new UIToolbar();
        toolbar.BarStyle = UIBarStyle.Default;
        toolbar.Translucent = true;
        toolbar.SizeToFit();

// Create a 'done' button for the toolbar and add it to the toolbar
        UIBarButtonItem doneButton = new UIBarButtonItem("Done", UIBarButtonItemStyle.Done,
            (s, e) => {
                Console.WriteLine ("Calling Done!");
                txt_RegistrationType.Text = selectedType;
                txt_Email.ResignFirstResponder();
                txt_UserName.ResignFirstResponder();
                txt_Password.ResignFirstResponder();
                txt_PhoneNumber.ResignFirstResponder();
                txt_ConformPassword.ResignFirstResponder();
            });
        toolbar.SetItems(new UIBarButtonItem[]{doneButton}, true);
        model.PickerChanged += (sender, e) => {
            Console.WriteLine("selected vlaue {0}",e.SelectedValue);
            txt_RegistrationType.Text = e.SelectedValue;
        };

this.myPickerView.AddSubview (toolbar);
myPickerView.Frame = PickerFrameWithSize (myPickerView.SizeThatFits (SizeF.Empty));
View.AddSubview (myPickerView);
myPickerView.AddSubview (toolbar);
}

点击选中的项目

它显示了pickerView" PickerView.Hidden = false"出现选择器视图和带有完成按钮的工具栏。单击工具栏上的“完成”按钮时,其单击事件不起作用。

请告诉我有关点击完成按钮的事件。

1 个答案:

答案 0 :(得分:0)

首先,我想指出您正在添加"工具栏"到你现在的" UIPickerView"两次。一次:

this.myPickerView.AddSubview (toolbar);

又一次:

myPickerView.AddSubview (toolbar);

其次,为什么工具栏?它只会增加项目的复杂性和依赖性。

我的建议是废弃整个代码块,并对此视图进行两个单独的控制:

  1. 按钮
  2. UIPickerView

    public override void ViewDidLoad()
    {
        UIButton button = new UIButton (new RectangleF (5, 5, frame_width, 30));
        button.TouchUpInside += Method;
    
        UIPickerView pickerView = new UIPickerView (new RectangleF (5, 45, frame_width, 180));
        pickerView.Model = model
    
        this.View.AddSubviews (new UIView[]{button, pickerView});
    }
    
    void Method (object sender, eventargs e)
    {
        Console.WriteLine ("Done");
        /*If you want to make the pickerView disappear, 
        you can add a pickerView.Hide() here, and then call a method 
        to redraw the controls on the current view.*/
    }
    
  3. 我希望这会有所帮助。祝你好运!