In Swift, how to get a UIAlertAction button to stay in the enclosure when pressed

82 views Asked by At

I have a UIAlertController that has a series of buttons. When I press the button, I want a variable to be set with a certain value which will in turn populate one of the textfields and the CoreData upon hitting the save button within the alert.

I am close but not quite there - when I press the button it will set the variable to what I want, but it immediately closes the alert. What I want to do is use keyboard to enter the information in the 2 textfields, press the button for 'vaccination' for example to set the entryType = "vaccination" and then the user should hit save to save the record.

  @IBAction func buttonAddEntry(_ sender: Any) {
    var typeField = ""
    let newEntry = Vaccination(context: context)
    
    print ("Create New VAX ENTRY")
    
    let alert = UIAlertController(title: "New Entry", message: "Enter Entry Details", preferredStyle: .alert)
    
    
    alert.addTextField()
    alert.addTextField()
    
    let dateField = alert.textFields![0]
    dateFieldMain = dateField
    //let typeField = alert.textFields![1]
    //let commentField = alert.textFields![2]
    
    let datePicker = UIDatePicker()
    datePicker.datePickerMode = UIDatePicker.Mode.date
    
    datePicker.addTarget(self, action: #selector(VaxViewController.datePickerValueChanged(sender:)), for: UIControl.Event.valueChanged)
    
    dateFieldMain!.inputView = datePicker
    
    print ("textfield set to datepicker")
    
    
    let bt1 = UIAlertAction(title: "Vaccination", style: UIAlertAction.Style.default) {
        (action) in
        print ("Vaccination")
        typeField = "Vaccination"
    }
    alert.addAction(bt1)
    
    let bt2 = UIAlertAction(title: "Wormer", style: UIAlertAction.Style.default){
        (action) in
        print ("Wormer")
        typeField = "Wormer"
    }
    alert.addAction(bt2)
    
    
    let bt3 = UIAlertAction(title: "Medication", style: UIAlertAction.Style.default){
        (action) in
        print ("Medication")
        typeField = "Medication"
    }
    alert.addAction(bt3)
    
    
    
    let cancelButton = UIAlertAction(title: "Cancel", style: UIAlertAction.Style.destructive) {
        (action) in return }
    alert.addAction(cancelButton)
    
    
    let alertButton = UIAlertAction(title: "Save", style: .default) { (action) in
        
        let dateField = alert.textFields![0]
        //let typeField = alert.textFields![1]
        let commentField = alert.textFields![2]
        
        //convert date
        let dateFormatter = DateFormatter()
        dateFormatter.dateFormat = "MMM, dd, yyyy"
        let dateConverted = dateFormatter.date(from: dateField.text!)
        
        //set the properties of the new entry
        newEntry.vaxDate1 = dateConverted
        newEntry.vaxType = typeField
        newEntry.comment = commentField.text
        newEntry.animal = self.animal
        
        try! self.context.save()
        
        self.fetchVaccinations()
        //self.tableView.reloadData()
    }
    
    
    alert.addAction(alertButton)
    
    self.present(alert, animated: true, completion: nil)
    
}
0

There are 0 answers