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

Print to the console what is currently selected with my 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)

    }