ios - 如何在swift 4中删除和添加uipickerview中的项目。?
<p><p>我有三个文本字段,我在其中显示与下拉菜单相同的选择器 View 以选择值。所以条件是我在一个数组中有 5 个值,其值为红色、蓝色、绿色、黄色、黑色。
所以条件是</p>
<ol>
<li><p>这三个文本字段不能具有相同的值。也就是说,对于第一个文本字段 1,如果我从选择器中选择红色作为值,则当我选择文本字段 2 或文本字段 3 时,应该从选择器 View 中删除或禁用值“红色”。</p></li>
<li><p>如果我从选取器 View 中将文本字段 1 的值从红色更改为黑色,则当我单击文本字段 2 或文本字段 3 时,禁用或删除的红色值应添加回选取器 View 。</p ></li>
</ol>
<p>我正在尝试的代码是:</p>
<pre><code>import UIKit
class ViewController: UIViewController, UIPickerViewDataSource, UIPickerViewDelegate {
@IBOutlet weak var textFiled1: UITextField!
@IBOutlet weak var textFiled2: UITextField!
@IBOutlet weak var textFiled3: UITextField!
@IBOutlet weak var pickerView: UIPickerView!
var Array = ["Blue", "Green", "Red", "White", "Grey"]
var indexOfPicker = Int()
override func viewDidLoad() {
super.viewDidLoad()
pickerView.dataSource = self
pickerView.delegate = self
}
@IBAction func minusButton(_ sender: UIButton) {
if Array.count != 0 {
Array.remove(at: indexOfPicker)
pickerView.reloadAllComponents()
}
}
@IBAction func plusButton(_ sender: UIButton) {
if textFiled.text != "" {
Array.append(textFiled.text!)
pickerView.reloadAllComponents()
}
}
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return Array.count
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
return Array
}
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
indexOfPicker = row
}
}
</code></pre></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><pre><code>class ViewController: UIViewController, UIPickerViewDataSource, UIPickerViewDelegate, UITextFieldDelegate {
@IBOutlet weak var textFiled1: UITextField!
@IBOutlet weak var textFiled2: UITextField!
@IBOutlet weak var textFiled3: UITextField!
@IBOutlet weak var pickerView: UIPickerView!
var selectedTextField:UITextField?
var colorsArray = ["Blue", "Green", "Red", "White", "Grey"]
override func viewDidLoad() {
super.viewDidLoad()
textFiled1.delegate = self
textFiled2.delegate = self
textFiled3.delegate = self
pickerView.dataSource = self
pickerView.delegate = self
}
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
let tempArr = colorsArray.filter { !.contains($0) }
return tempArr.count
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
let tempArr = colorsArray.filter { !.contains($0) }
return tempArr
}
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
let tempArr = colorsArray.filter { !.contains($0) }
self.selectedTextField?.text = tempArr
pickerView.reloadAllComponents()
}
func textFieldDidBeginEditing(_ textField: UITextField) {
self.selectedTextField = textField
pickerView.reloadAllComponents()
}
}
</code></pre></p>
<p style="font-size: 20px;">关于ios - 如何在swift 4中删除和添加uipickerview中的项目。?,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/55015563/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/55015563/
</a>
</p>
页:
[1]