Search code examples
iosswiftxcodeuipickerviewuialertcontroller

How to pause the code and wait for an answer from UIAlert on Swift using UIAlertController


I got to say I've searched around for the answer in stack overflow, but couldn't find it so that's why I'm asking it...

**

  • MyProblem: my problem is - I'm making a Pomodoro App, and I let the user to set the TimeLabel(the time), now if the user is in a session, and he wants to change the time he's able doing that, although if he tries doing that and once he has changed the time and wanna cancel it, the user of-course is able to do that, now the problem is I'm waiting for the user's answer by using UiAlertController, but among that I also use my function that updates the Settings Labels/Parameters.

**

My Code: my update settings function:

//Updating the UserSettings:
func updateSettingsLabels(cell: PickerTableViewCell, rowIndex: Int) {
    let identifier = cell.reuseIdentifier

    switch identifier {
    case T.Identifiers.workIntervalID:
        workInterval.text = timesDictionary[rowIndex]
        self.workIntervalString = timesDictionary[rowIndex] // set string
        
        let myString = workInterval.text!
        oldTime = workInterval.text! // setting up the old time
        
        saveTime(labelText: myString)
        
    case T.Identifiers.shortBreakID:
        shortBreak.text = timesDictionary[rowIndex]
        self.shortBreakString = timesDictionary[rowIndex] // set string
        saveShortBreak(breakText: shortBreak.text!)
        
    case T.Identifiers.longBreakTimeID:
        longBreakTime.text = timesDictionary[rowIndex]
        self.longBreakString = timesDictionary[rowIndex] // set string
        saveLongBreak(longBreakText: longBreakString!)
        
        
    case T.Identifiers.breakAfterIntervalsID: // after how much intervals we will take a long break
        longBreakAfter.text = intervalsDictionary[rowIndex]
        self.longBreakAfterIntervals = intervalsDictionary[rowIndex] // set string
        saveBreakAfterIntervals(breakAfterIntervals: longBreakAfterIntervals!)

        
    case T.Identifiers.dailyIntervalsID: // daily intervals
        dailyIntervals.text = intervalsDictionary[rowIndex]
        self.dailyIntervalsString = intervalsDictionary[rowIndex] // set string
        saveDailyIntervals(dailyIntervals: dailyIntervalsString!)
        
    
    default:
        print("There was a problem with Switch")
    }
    
    

my UiAlert Function:

func alertTheUser() {
    //If we want to print something on the screen, we use UIAlertController:
    let alert = UIAlertController(title: "Do you want to aboard this task?", message: "Once you change the time in a middle of a session, you will reset all tasks.", preferredStyle: .alert)
    
    
    alert.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: { (alertToCancel) in
        
        self.userWantToChangeSettings = false
        print("Im being changed!")
    }))

    alert.addAction(UIAlertAction(title: "Ok", style: .destructive, handler: { (action) in

        self.userWantToChangeSettings = true
    }))
    
    
    self.present(alert, animated: true)
}

I must inform u guys that I also use UIPicker that lets the user choose the time from optional times, and here's my code for that:

func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int, forCell cell: PickerTableViewCell) {

    
    if self.timerIsOnFire == true {
            self.alertTheUser()
            
    }
    
    
    updateSettingsLabels(cell: cell, rowIndex: row)
    
    
    self.delegate?.userSettings(interval: self.workIntervalString, shortBreak:self.shortBreakString, longBreak:self.longBreakString, breakAfterIntervals:self.longBreakAfterIntervals,dailyIntervals:self.dailyIntervalsString )
    
    
    self.view.endEditing(true)
    
}

Solution

  • Use your Alert function with completion handler like this

    func alertTheUser(completion:@escaping ()->Void) {
        //If we want to print something on the screen, we use UIAlertController:
        let alert = UIAlertController(title: "Do you want to aboard this task?", message: "Once you change the time in a middle of a session, you will reset all tasks.", preferredStyle: .alert)
        
        
        alert.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: { (alertToCancel) in
            
            self.userWantToChangeSettings = false
            print("Im being changed!")
             completion()
        }))
    
        alert.addAction(UIAlertAction(title: "Ok", style: .destructive, handler: { (action) in
    
            self.userWantToChangeSettings = true
            completion()
        }))
        
        
        self.present(alert, animated: true)
    }