Попытка создать несколько кнопок и несколько меток, чтобы при нажатии кнопки всплывал контроллер предупреждений (с текстовым полем, кнопкой ОК, кнопкой очистки и кнопкой отмены).
Я пытаюсь минимизировать количество кода, поэтому я сделал коллекцию меток:
@IBOutlet var textFieldLabel: [UILabel]!
@IBAction func textFieldTapped(_ sender: UIButton) {
print("Other11 Button Tapped")
openOther11Alert()
}
func openOther11Alert() {
//Create Alert Controller
let alert = UIAlertController (title: "Other:", message: nil, preferredStyle: UIAlertControllerStyle.alert)
//Create Clear Action
let bt = UIAlertAction(title: "CLEAR", style: UIAlertActionStyle.destructive){
(action) in self.textFieldLabel[0].text = ""}
alert.addAction(bt)
//Create Cancel Action
let cancel = UIAlertAction(title: "Cancel", style: UIAlertActionStyle.cancel, handler: nil)
alert.addAction(cancel)
//Create OK Action
let ok = UIAlertAction(title: "OK", style: UIAlertActionStyle.default) { (action: UIAlertAction) in print("OK")
let textfield = alert.textFields?[0]
print(textfield?.text! as Any)
self.textFieldLabel[0].text = textfield?.text!
}
alert.addAction(ok)
//Add Text Field
alert.addTextField { (textfield: UITextField) in
textfield.text = self.textFieldLabel[0].text
textfield.placeholder = "Other"
}
//Present Alert Controller
self.present(alert, animated:true, completion: nil)
}
EDIT:
Я хочу, чтобы кнопка нажата, чтобы соответствовать метке. В настоящее время я ссылаюсь только на первый элемент (метку) в массиве меток с помощью:
self.textFieldLabel[0].text
Вместо ссылки «[0]» (первая метка) я хочу сослаться на кнопку, которая была нажата, так, чтобы кнопка соответствовала метке.
Есть идеи, если я смогу сделать то, что пытаюсь?
Заранее спасибо! :)