选择器没有快速输出行数据 3
Posted
技术标签:
【中文标题】选择器没有快速输出行数据 3【英文标题】:Picker not outputting row data swift 3 【发布时间】:2018-06-21 15:28:33 【问题描述】:我正在尝试为任务应用程序编写颜色选择器。当我尝试添加任务时,选择器永远不会更改我的“picked”变量(在第 5 行声明),只会打印“空白”(第 7 行)。我对采摘者来说足够新,我看不出我做错了什么。我相当确定它与我的 didSelectRow 函数(最后一个函数)的设置有关,因为如果该函数被触发,它仍然会在 else 语句中设置一些东西,即使它不是我想要的字符串,但它不是进行任何更改。
class SecondViewController: UIViewController, UIPickerViewDelegate, UIPickerViewDataSource
@IBOutlet weak var input: UITextField!
@IBOutlet weak var picker: UIPickerView!
var pickerData: [String] = [String]()
var picked: String = "blank"
@IBAction func addItem(_ sender: Any)
print (picked)
if (input.text != "" && picked != "")
list.append(input.text! as AnyObject)
if picked == "Red"
colors.append(UIColor.red)
else if picked == "Orange"
colors.append(UIColor.orange)
else if picked == "Yellow"
colors.append(UIColor.yellow)
else if picked == "Green"
colors.append(UIColor.green)
else if picked == "Blue"
colors.append(UIColor.blue)
else if picked == "Purple"
colors.append(UIColor.purple)
else if picked == "White"
colors.append(UIColor.white)
input.text = ""
userDefaults.set(list, forKey: "tasks")
override func viewDidLoad()
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
self.picker.delegate = self
self.picker.dataSource = self
pickerData = ["Select a color", "White", "Red", "Orange", "Yellow", "Green", "Blue", "Purple"]
override func didReceiveMemoryWarning()
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
func numberOfComponents(in pickerView: UIPickerView) -> Int
return 1
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int
return pickerData.count
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String?
return pickerData[row]
func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int)
if row == 0
picked = ""
else
picked = pickerData[row]
【问题讨论】:
【参考方案1】:你忘记了参数标签_
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int)
【讨论】:
谢谢!以上是关于选择器没有快速输出行数据 3的主要内容,如果未能解决你的问题,请参考以下文章