I have a IBAction button which makes a calculation. In this IBAction, I want to implement an UIAlertController button where the user can press "OK" or "Cancel" to confirm a recalculation.
The Problem is that asap IBAction button is pressed, the calculation is done.
How can I make the function wait for UIAlertController
response.
@IBAction func buttonCalculate(_ sender: UIButton) {
let startDate = datePickerStart.date
let endDate = datePikcerEnd.date
// Calculate DAYS between start and end date
var diff = Calendar.current.dateComponents([.day], from: startDate, to: endDate).day!
//calculate ALL between start and end date
var daily = 6
diff = 1
daily = daily*diff
// If "Calculate" button is pressed for recalculationhen
let emptyDiffCalcDaysLabel = diffCalcDaysLabel.text
let emptyAmountLabel = allAmount.text
if emptyDiffCalcDaysLabel != "-" || emptyAmountLabel != "-"
{
let alertController = UIAlertController(title: "Attention", message: "Everything will be recalculated", preferredStyle: .alert)
let defaultAction = UIAlertAction(title: "Ok", style: .default) {(ACTION) in print("OK")}
let cancelAction = UIAlertAction(title: "Cancel", style: .cancel) {(ACTION) in print("Cancel")}
alertController.addAction(defaultAction)
alertController.addAction(cancelAction)
self.present(alertController, animated: true)
}
//Alert when negative value is calculated
if diff < 1
{
let negativeDaysUIAlert = UIAlertController(title: "Attention", message: "Entered Date is invalid!", preferredStyle: .alert)
negativeDaysUIAlert.addAction(UIAlertAction(title: "OK", style: .default))
self.present(negativeDaysUIAlert, animated: true, completion: nil)
// Show "-" instead of negative value
diffCalcDaysLabel.text = "-"
allAmount.text = "-"
}
}
CodePudding user response:
You need to put in the IBAction 'ok' callback, the code to recalculate (or a call the the function that recalculate).
Like that the recalculation is only done when the user click ok.