我有一个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)
}