簡體   English   中英

如何使用 UIAlertController 在 Swift 上暫停代碼並等待 UIAlert 的回答

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

我不得不說我已經在堆棧溢出中搜索了答案,但找不到它,所以這就是我問它的原因......

**

  • MyProblem:我的問題是 - 我正在制作番茄鍾應用程序,我讓用戶設置 TimeLabel(時間),現在如果用戶在 session 中,他想改變他能夠做到這一點的時間,雖然如果他嘗試這樣做並且一旦他改變了時間並想取消它,用戶當然可以這樣做,現在問題是我正在使用 UiAlertController 等待用戶的回答,但其中我也使用我的 function 更新設置標簽/參數。

**

我的代碼:我的更新設置 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")
    }
    
    

我的 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)
}

我必須通知你們,我也使用 UIPicker 讓用戶從可選時間中選擇時間,這是我的代碼:

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)
    
}

將您的警報 function 與這樣的完成處理程序一起使用

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)
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM