将当前使用我的 uipickerView 选择的内容打印到控制台?



这是我的代码:

class WelcomeViewController: UIViewController, UIPickerViewDelegate, UIPickerViewDataSource {
@IBOutlet weak var locationPicker: UIPickerView!
override func viewDidLoad() {
super.viewDidLoad()
locationPicker.delegate = self
locationPicker.dataSource = self
}
var locationData = ["San Fransisco", "New York", "London", "Paris", "Rio", "Bejing"]
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return locationData.count
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
return locationData[row]
}
}

我想将当前选择的位置打印到控制台,以便每个位置都可以将用户发送到不同的屏幕。

你需要 UIPickerViewDelegate 的 didSelectRow 方法。

func pickerView(pickerView: UIPickerView!, didSelectRow row: Int, inComponent component: Int)
{
let location = locationData[row]
print(location)
}

最新更新