使用此链接TextInput Field with Pickerview instead of keyboard上的答案作为指南,我编写了以下代码。我有三个UITextFields,需要配置一个选取器作为输入。第二个依赖于第一个的选择,第三个依赖于第二个的选择。由于每个UITextField需要不同的数据,我对如何将数据放入class PickerViewModel感到有点困惑。代码和我的修改:
void ConfigurePicker(UITextField pickerTextField)
{
// var pickerTextField = new UITextField();
List<string> theData = new List<string>();
if ((pickerTextField.Placeholder.ToUpper() == "SELECT COMPANY" && dropTextField.Text == "") )
{
var okAlertController = UIAlertController.Create("ERROR", "Please select a company from the company drop down liat.", UIAlertControllerStyle.Alert);
okAlertController.AddAction(UIAlertAction.Create("OK", UIAlertActionStyle.Default, null));
PresentViewController(okAlertController, true, null);
}else if (pickerTextField.Placeholder.ToUpper() == "SELECT SECTION" && dropTextField2.Text == "")
{
var okAlertController = UIAlertController.Create("ERROR", "Please select a department from the department drop down liat.", UIAlertControllerStyle.Alert);
okAlertController.AddAction(UIAlertAction.Create("OK", UIAlertActionStyle.Default, null));
PresentViewController(okAlertController, true, null);
}
else
{
var picker = new UIPickerView
{
Model = new PickerViewModel(),
ShowSelectionIndicator = true
};
var screenWidth = UIScreen.MainScreen.Bounds.Width;
var pickerToolBar = new UIToolbar(new RectangleF(0, 0, (float)screenWidth, 44)) { BarStyle = UIBarStyle.Default, Translucent = true };
var flexibleSpaceButton = new UIBarButtonItem(UIBarButtonSystemItem.FlexibleSpace);
var doneButton = new UIBarButtonItem(UIBarButtonSystemItem.Done, (sender, e) => pickerTextField.ResignFirstResponder());
pickerToolBar.SetItems(new[] { flexibleSpaceButton, doneButton }, false);
pickerTextField.InputView = picker;
pickerTextField.InputAccessoryView = pickerToolBar;
}
}
class PickerViewModel : UIPickerViewModel
{
public List<string> _pickerSource = new List<string>();
public override nint GetRowsInComponent(UIPickerView pickerView, nint component) => _pickerSource.Count;
public override string GetTitle(UIPickerView pickerView, nint row, nint component) => _pickerSource[(int)row];
public override nint GetComponentCount(UIPickerView pickerView) => 1;
}发布于 2020-07-03 07:11:05
在我接孙子上班的时候我得到了答案。这里有一个很大的"Duh“...实例化模型,将属性_pickerSource设置为数据,将选择器视图模型设置为MyModel ...很简单……
void ConfigurePicker(UITextField pickerTextField, List<string> theData)
{
if ((pickerTextField.Placeholder.ToUpper() == "SELECT COMPANY" && dropTextField.Text == "") )
{
var okAlertController = UIAlertController.Create("ERROR", "Please select a company from the company drop down liat.", UIAlertControllerStyle.Alert);
okAlertController.AddAction(UIAlertAction.Create("OK", UIAlertActionStyle.Default, null));
PresentViewController(okAlertController, true, null);
}else if (pickerTextField.Placeholder.ToUpper() == "SELECT SECTION" && dropTextField2.Text == "")
{
var okAlertController = UIAlertController.Create("ERROR", "Please select a department from the department drop down liat.", UIAlertControllerStyle.Alert);
okAlertController.AddAction(UIAlertAction.Create("OK", UIAlertActionStyle.Default, null));
PresentViewController(okAlertController, true, null);
}
else
{
PickerViewModel MyModel = new PickerViewModel();
MyModel._pickerSource = theData;
var picker = new UIPickerView
{
Model = MyModel,
ShowSelectionIndicator = true
};
var screenWidth = UIScreen.MainScreen.Bounds.Width;
var pickerToolBar = new UIToolbar(new RectangleF(0, 0, (float)screenWidth, 44)) { BarStyle = UIBarStyle.Default, Translucent = true };
var flexibleSpaceButton = new UIBarButtonItem(UIBarButtonSystemItem.FlexibleSpace);
var doneButton = new UIBarButtonItem(UIBarButtonSystemItem.Done, (sender, e) => pickerTextField.ResignFirstResponder());
pickerToolBar.SetItems(new[] { flexibleSpaceButton, doneButton }, false);
pickerTextField.InputView = picker;
pickerTextField.InputAccessoryView = pickerToolBar;
}
}
class PickerViewModel : UIPickerViewModel
{
public List<string> _pickerSource = new List<string>();
public override nint GetRowsInComponent(UIPickerView pickerView, nint component) => _pickerSource.Count;
public override string GetTitle(UIPickerView pickerView, nint row, nint component) => _pickerSource[(int)row];
public override nint GetComponentCount(UIPickerView pickerView) => 1;
} https://stackoverflow.com/questions/62704337
复制相似问题