ViewController的单触式UIPickerView ValueChange事件
本文关键字:ValueChange 事件 UIPickerView 单触式 ViewController | 更新日期: 2023-09-27 17:58:34
我想在UIPickerView
更改其任何组件中的值时获取事件,以便我的UIViewController
可以更改Labels
。我只遇到了PickerView
模型提供的一种有用的方法:
public override void Selected(UIPickerView pickerView, nint row, nint component)
当任何东西发生变化时,都会有相应的变化反映在模型中。但是,如果我想将这些更改反映回ViewController
,我该怎么办?我想像iOS中的Delegates那样做这项工作。我对c#不太熟悉。感谢您的帮助。谢谢
public partial class ViewController : UIViewController
{
public override void ViewDidLoad()
{
base.ViewDidLoad();
// Perform any additional setup after loading the view, typically from a nib.
setupHandlers();
}
void setupHandlers()
{
pickerModel = new UnitPickerViewModel();
pickerModel.Items = lengthArray;
pickerModel.setDataForSecondComponent(lengthArray2);
pickerView = new UIPickerView();
pickerView.BackgroundColor = UIColor.LightGray;
pickerView.DataSource = pickerModel;
}
}
您可以创建弱和强Obj-C风格的委托:
强有力的代表:
在您的UIViewController
类中创建嵌套的UIPickerViewDelegate
类:
class PickerDelegate : UIPickerViewDelegate
{
public override void Selected(UIPickerView pickerView, nint row, nint component)
{
base.Selected(pickerView, row, component);
Console.WriteLine(row.ToString());
}
}
创建代表并将其分配给您的UIPickerView
:
var picker = new UIPickerView(UIScreen.MainScreen.Bounds);
picker.DataSource = this;
picker.Delegate = new PickerDelegate();
弱势代表:
跳过嵌套类,将UIPicker选择的方法添加到UIViewController
中,并用Foundation.Export
:标记它
[Export("pickerView:didSelectRow:inComponent:")]
public void Selected(UIPickerView pickerView, nint row, nint component)
{
Console.WriteLine(row.ToString());
}
将你的UIViewController
指定为弱势代表:
var picker = new UIPickerView(UIScreen.MainScreen.Bounds);
picker.DataSource = this;
picker.WeakDelegate = this;
Add(picker);