Basically, i have an alert controller set up so that when i click a button in the view controller, an alert controller pops up and i can type words into a textfield, click an "OK" button.
Reference image
(source: ashfurrow.com)
and it inserts that text into a label in the view controller. Ive extended this ability so that i can have preset keywords (like "Good," "Likely" and "Almost") that i can select from in the alert controller to speed up the typing process, as these words are typed a lot in my app. which is shown in the below image
.
I was wondering if you could also add those key words that I've selected to whatever is in the label? Every time i try to click a preset keyword to add to the label, it erases whatever was already in the label.
Specifically, whatever text that is already in my label, can i select a preset keyword to append to whats in the label?
This is what i have so far, for the second image:
//Editable Text Box with Preset Keywords
@IBOutlet weak var UselessLabel: UILabel!
@IBAction func UselessTapped(_ sender: UIButton) {
print("Useless Button Tapped")
openUselessAlert()
}
func openUselessAlert() {
//Create Alert Controller
let alert9 = UIAlertController (title: "Uselss:", message: nil, preferredStyle: UIAlertControllerStyle.alert)
//preset keyword as button in alert controller
let bt1 = UIAlertAction(title: "Good", style: UIAlertActionStyle.default){
(action) in self.UselessLabel.text = "Good"}
alert9.addAction(bt1)
//preset keyword as button in alert controller
let bt2 = UIAlertAction(title: "Likely", style: UIAlertActionStyle.default){
(action) in self.UselessLabel.text = "Likely"}
alert9.addAction(bt2)
//preset keyword as button in alert controller
let bt3 = UIAlertAction(title: "Almost", style: UIAlertActionStyle.default){
(action) in self.UselessLabel.text = "Almost"}
alert9.addAction(bt3)
//Create Cancel Action
let cancel9 = UIAlertAction(title: "CANCEL", style: UIAlertActionStyle.cancel, handler: nil)
alert9.addAction(cancel9)
//Create OK Action
let ok9 = UIAlertAction(title: "OK", style: UIAlertActionStyle.default) { (action: UIAlertAction) in print("OK")
let textfield = alert9.textFields?[0]
print(textfield?.text!)
self.UselessLabel.text = textfield?.text!
}
alert9.addAction(ok9)
//Add Text Field
alert9.addTextField { (textfield: UITextField) in
textfield.text = self.UselessLabel.text
textfield.placeholder = "Useless"
}
//Present Alert Controller
self.present(alert9, animated:true, completion: nil)
}
Please help! How exactly would you do this in swift 3?
To add the action text to a UILabel you can use
let bt3 = UIAlertAction(title: "Almost", style: UIAlertActionStyle.default){
(action) in self.UselessLabel.text = self.UselessLabel.text + action.title}
and if you want to add to UITextField in the AlertView then you must do something like this
let bt3 = UIAlertAction(title: "Almost", style: UIAlertActionStyle.default){
(action) in if(alert9.textFields.count > 0)
{
alert9.textFields[0].text = alert9.textFields[0].text + action.title
}
}
I hope this helps you