我有三个文本字段,我在其中显示与下拉菜单相同的选择器 View 以选择值。所以条件是我在一个数组中有 5 个值,其值为红色、蓝色、绿色、黄色、黑色。 所以条件是
这三个文本字段不能具有相同的值。也就是说,对于第一个文本字段 1,如果我从选择器中选择红色作为值,则当我选择文本字段 2 或文本字段 3 时,应该从选择器 View 中删除或禁用值“红色”。
如果我从选取器 View 中将文本字段 1 的值从红色更改为黑色,则当我单击文本字段 2 或文本字段 3 时,禁用或删除的红色值应添加回选取器 View 。
我正在尝试的代码是:
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[row]
}
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
indexOfPicker = row
}
}
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 { ![textFiled1.text!,textFiled2.text!,textFiled3.text!].contains($0) }
return tempArr.count
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
let tempArr = colorsArray.filter { ![textFiled1.text!,textFiled2.text!,textFiled3.text!].contains($0) }
return tempArr[row]
}
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
let tempArr = colorsArray.filter { ![textFiled1.text!,textFiled2.text!,textFiled3.text!].contains($0) }
self.selectedTextField?.text = tempArr[row]
pickerView.reloadAllComponents()
}
func textFieldDidBeginEditing(_ textField: UITextField) {
self.selectedTextField = textField
pickerView.reloadAllComponents()
}
}
关于ios - 如何在swift 4中删除和添加uipickerview中的项目。?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55015563/
欢迎光临 OStack程序员社区-中国程序员成长平台 (https://ostack.cn/) | Powered by Discuz! X3.4 |