【发布时间】:2020-07-02 20:12:27
【问题描述】:
使用此链接TextInput Field with Pickerview instead of keyboard 上的答案作为指导,我编写了以下代码。我有三个 UITextFields 我需要配置一个选择器作为输入。第二个取决于第一个的选择,第三个取决于第二个的选择。我对如何将数据输入class PickerViewModel 有点茫然,因为每个 UITextField 都需要不同的数据。我修改的代码:
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;
}
【问题讨论】:
标签: c# uitextfield uipickerview