gpt4 book ai didi

ios - Monotouch 获取 UiPickerView 选中项

转载 作者:行者123 更新时间:2023-11-28 18:37:41 27 4
gpt4 key购买 nike

我在操作表中以编程方式制作了一个自定义 uipicker,现在我需要设置一个文本字段,其中包含选择器的所选项目。使用日期选择器,我得到这样的值:

 actionSheetDatePicker.DatePicker.ValueChanged += (s, e) => {
this.lblCheckOut.Text = f.ToString ((s as UIDatePicker).Date);
};

现在我正尝试用我的 UIPickerView 做同样的事情,但我没有 ValueChanged 方法。

有没有人能帮我得到用我的选择器选择的项目的值(value)。

提前致谢。

最佳答案

为了详细说明 Jason 的回答,这里有一个使用 UIPickerView 作为自定义日期选择器的代码示例。希望它能帮助别人。

UIPickerView datePicker = new UIPickerView ();
var mod = new DatePickerDataModel ();
datePicker.Model = mod;

mod.ValueChanged += (object sender, EventArgs e) => {
_selectedDate = mod.SelectedItem;
};


class DatePickerDataModel : UIPickerViewModel
{
public event EventHandler<EventArgs> ValueChanged;

public List<DateTime> Items { get; private set; }

public DateTime SelectedItem {
get { return Items [selectedIndex]; }
}

int selectedIndex = 0;

public DatePickerDataModel ()
{
var items = new List<DateTime> ();
for (int i = 0; i < 14; i++) {
items.Add (DateTime.Now.Subtract (TimeSpan.FromDays (i)));
}
Items = items;
}

public override nint GetRowsInComponent (UIPickerView picker, nint component)
{
return Items.Count;
}

public override nint GetComponentCount (UIPickerView picker)
{
return 1;
}

public override void Selected (UIPickerView picker, nint row, nint component)
{
selectedIndex = (int)row;
ValueChanged?.Invoke(this, EventArgs.Empty());
}

public override string GetTitle (UIPickerView pickerView, nint row, nint component)
{
var t = Items [(int)row];
var m = t.ToString ("MMM");
var d = t.ToString ("ddd");
return $"{d} {m} {t.Day}";
}

public override nfloat GetRowHeight (UIPickerView picker, nint component)
{
return 44;
}
}

关于ios - Monotouch 获取 UiPickerView 选中项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15976573/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com