2 个 TextField,在 1 个 UIView 中有 2 个选择器
Posted
技术标签:
【中文标题】2 个 TextField,在 1 个 UIView 中有 2 个选择器【英文标题】:2 TextField with 2 picker in 1 UIView 【发布时间】:2020-02-05 12:44:23 【问题描述】:我在 1UIView 中创建了一个带有 2 个选择器的 2 个文本字段,但我无法在正确的文本字段中显示正确的选择器,因为我不知道如何识别当前处于活动状态的视图。 我尝试了一些带有标签的东西(但只能使用选择器,不能使用文本字段),并创建 var current = UITextField()
var genre = ["-","Homme","Femme"]
var Sport = ["-","Tennis"]
var picker = UIPickerView()
var currentTextField = UITextField()
override func viewDidLoad()
super.viewDidLoad()
picker.delegate = self
picker.dataSource = self
GenderTextField.inputView = picker
WitchSportTextField.inputView = picker
func numberOfComponents(in pickerView: UIPickerView) -> Int
return 1
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int
if currentTextField == GenderTextField
return genre.count
else
return Sport.count
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int)
if currentTextField == GenderTextField
GenderTextField.text = genre[row]
else
WitchSportTextField.text = Sport[row]
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String?
if currentTextField == GenderTextField
return genre[row]
else
return Sport[row]
某人作为一个想法?
【问题讨论】:
使用 TextField 委托。 【参考方案1】:这是完整的代码
var genre = ["-","Homme","Femme"]
var Sport = ["-","Tennis"]
var genrepicker = UIPickerView()
var Sportpicker = UIPickerView()
var genretTextField = UITextField()
var SportTextField = UITextField()
override func viewDidLoad()
super.viewDidLoad()
genrepicker.delegate = self
genrepicker.dataSource = self
Sportpicker.delegate = self
Sportpicker.dataSource = self
genretTextField.inputView = genrepicker
SportTextField.inputView = Sportpicker
func numberOfComponents(in pickerView: UIPickerView) -> Int
return 1
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int
if pickerView == genrepicker
return genre.count
else
return Sport.count
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int)
if pickerView == genrepicker
GenderTextField.text = genre[row]
else
WitchSportTextField.text = Sport[row]
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String?
if pickerView == genrepicker
return genre[row]
else
return Sport[row]
【讨论】:
【参考方案2】:首先将您的两个 TextField 委托分配给 viewDidLoad
中的 self
GenderTextField.delegate = self
WitchSportTextField.delegate = self
使用此扩展程序来确定哪个 TextField 处于活动状态。
extension YourClass: UITextFieldDelegate
func textFieldDidBeginEditing(_ textField: UITextField)
currentTextField = textField
picker.reloadAllComponents() //Reload for accurate data
注意:变量名应以小写字母开头。使用描述性名称来阐明您的意图:
看看这个:https://github.com/MLSDev/development-standards/blob/master/platform/ios/swift-coding-conventions.md
【讨论】:
以上是关于2 个 TextField,在 1 个 UIView 中有 2 个选择器的主要内容,如果未能解决你的问题,请参考以下文章
在 secondViewController 中有 4 个 textField 。我想用 firstviewController 集合视图单元检查这个 textField 值
让 Django 仅返回存储为 models.TextField() 的文本的前 50 个字符