如果創建@IBOutlet
s到你的兩個pickerViews:
@IBOutlet weak var picker1: UIPickerView!
@IBOutlet weak var picker2: UIPickerView!
您可以訪問選定行的每個:
let row1 = picker1.selectedRow(inComponent: 0)
let row2 = picker2.selectedRow(inComponent: 0)
然後,您可以用這些作爲指標到屬於陣列爲您的採摘者提供數據。
舉個例子,我創建了兩個採摘期,我打印的總和每當採摘變化之一:
class ViewController: UIViewController, UIPickerViewDelegate, UIPickerViewDataSource {
@IBOutlet weak var picker1: UIPickerView!
@IBOutlet weak var picker2: UIPickerView!
var data = [1, 2, 3, 4, 5]
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
let sum = data[picker1.selectedRow(inComponent: 0)] + data[picker2.selectedRow(inComponent: 0)]
print(sum)
}
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return data.count
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
return String(data[row])
}
}
我有完全在你那裏,除了我需要有一個seprate功能是什麼用於計算/轉換,因爲它必須連接到API並將字符串轉換爲我已有的textField中的數字。我需要的只是從pickerViews中選擇的數據中獲得標題,並在顯示屏底部的標籤中顯示計算結果。 –
你明白我在說什麼,或者你想讓我更深入嗎? –
所以我只需要從那個didSelect函數中選中的數據的名稱顯示在另一個函數中,但我有一個範圍問題,並不知道如何解決它。 –