gpt4 book ai didi

arrays - 在 'UIPickerView' 功能设置上接收错误

转载 作者:行者123 更新时间:2023-11-28 14:38:42 25 4
gpt4 key购买 nike

我在一个应用程序中有两个(或更多)文本字段,我希望每个文本字段在被选中时弹出它自己的 Pickerview 轮...这是我到目前为止所得到的:

var pickerView = UIPickerView()

@IBOutlet weak var serviceType: UITextField!
@IBOutlet weak var brandsField: UITextField!

var serviceArray = ["Services", "..."]
var brandsArray = ["Apple", "Samsung"]

override func viewDidLoad() {
super.viewDidLoad()

pickerView.delegate = self
pickerView.dataSource = self
}

func updatePicker(){
self.pickerView.reloadAllComponents()
}

func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 1
}

func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
if serviceType.isFirstResponder{
return serviceArray.count
} else if brandsField.isFirstResponder{
return brandsArray.count
}
} //ERROR HERE

func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
if serviceType.isFirstResponder{
return serviceArray[row]
}else if brandsField.isFirstResponder{
return brandsArray[row]
}
} //ERROR HERE

func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
if serviceType.isFirstResponder{
let itemselected = serviceArray[row]
serviceType.text = itemselected
}else if brandsField.isFirstResponder{
let itemselected = brandsArray[row]
brandsField.text = itemselected
}
}

}
在我评论过的地方。我能做什么?

最佳答案

如果你想在 pickerview 中使用多个文本框,你可以使用这个 UITextField 子类。

class PickerTextField: UITextField,UIPickerViewDelegate,UIPickerViewDataSource {

let pickerView = UIPickerView()
var itemList = [String]()

override init(frame: CGRect) {
super.init(frame: frame)
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
@objc func textEdited(_ sender:PickerTextField)
{
self.text = itemList[pickerView.selectedRow(inComponent: 0)]
}

override func draw(_ rect: CGRect) {
super.draw(rect)
self.tintColor = UIColor.clear
self.addTarget(self, action: #selector(textEdited(_:)), for: .editingChanged)
pickerView.showsSelectionIndicator = true
pickerView.delegate = self
pickerView.dataSource = self
self.inputView = pickerView

let toolBar = UIToolbar()
toolBar.barStyle = UIBarStyle.default
toolBar.isTranslucent = true
toolBar.tintColor = .black
toolBar.sizeToFit()

let doneButton = UIBarButtonItem(title: "Done", style: .plain, target: self, action: #selector(doneBtnAction(_:)))
let spaceButton = UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: nil, action: nil)
let cancelButton = UIBarButtonItem(title: "Cancel", style: .plain, target: self, action: #selector(doneBtnAction(_:)))

toolBar.items = [cancelButton, spaceButton, doneButton]
self.inputAccessoryView = toolBar
}
@objc func doneBtnAction(_ sender:UIBarButtonItem) {
resignFirstResponder()
}
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return itemList.count
}
func pickerView(_ pickerView: UIPickerView, attributedTitleForRow row: Int, forComponent component: Int) -> NSAttributedString? {
let title = itemList[row]
return NSAttributedString(string: title, attributes: [NSAttributedStringKey.foregroundColor:UIColor.black])
}
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
self.text = itemList[row]
}
}

并在viewDidLoad中分配选项列表

class ViewController: UIViewController {

@IBOutlet weak var serviceType: PickerTextField!
@IBOutlet weak var brandsField: PickerTextField!

override func viewDidLoad() {
super.viewDidLoad()
serviceType.itemList = ["Services", "others"]
brandsField.itemList = ["Apple", "Samsung"]
}
}

将子类分配给文本字段。

enter image description here

结果

enter image description here

关于arrays - 在 'UIPickerView' 功能设置上接收错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50685441/

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