防止将空白项目附加到UIPickerView

时间:2019-05-03 01:47:02

标签: swift uipickerview

我有一个UIPickerView,可以由用户附加。我有一个alert和一个UITextField,可以接受这些添加。但是,如果用户未输入添加项,则空白点仍将附加到阵列中,并且在UIPickerView中可用。

这是alert进行加法时运行的代码。

@IBAction func addLanguageButtonPressed(_ sender: UIButton) {
        var textField = UITextField()

        let alert = UIAlertController(title: "Add A Language", message: "", preferredStyle: .alert)

        alert.addAction(UIAlertAction(title: "Add Language", style: .default, handler: { (action) in
            if textField.text == nil {
                alert.dismiss(animated: true, completion: nil)}
            else {
            self.languagePickerArray.append(textField.text!)
            self.defaults.set(self.languagePickerArray, forKey: "languages")
            }
        }))
        alert.addAction(UIAlertAction(title: "Dismiss", style: .default, handler: { (action) in
            alert.dismiss(animated: true, completion: nil)
        }))
        alert.addTextField { (alertTextField) in
            alertTextField.placeholder = "Add a language"
            textField = alertTextField
        }
        present(alert, animated: true, completion: nil)
}

0 个答案:

没有答案