簡體   English   中英

在繼續執行代碼之前,如何檢查UIAlertController是否已被關閉?

[英]How do I check if UIAlertController has been dismissed before continuing to execute code?

在繼續執行代碼之前,如何檢查UIAlertController是否已被關閉?

這是我的計時器之一。

func Counting1(){
    timerCount+=1
    timerLabel.text="\(timerCount) secs"

    if timerRunning==true && timerCount >= timerMaximum {
        stop()
        intervalAlert()

        // Check if UIAlertController has been dismissed before continuing to start2()


        start2()
    }

}

這是我的一個UIAlertControllers的示例。

func configureWorkoutAlert(){
    let title = "Oh no!"
    let message = "You have to configure a workout before starting one!"
    let okText = "Okay."

    let alert=UIAlertController(title: title, message: message, preferredStyle: UIAlertControllerStyle.Alert)

    let okayButton = UIAlertAction(title: okText, style: UIAlertActionStyle.Cancel, handler: nil)
    alert.addAction(okayButton)



    presentViewController(alert, animated: true, completion: nil)

}

我想等到UIAlertController被解雇后再繼續執行代碼...

這可能有幫助

if (buttonIndex != [alertView cancelButtonIndex]) {
    NSLog(@"Someone pressed OK");
}

我需要查看代碼以提供精確的if子句。

首先在類中定義變量:

var alert:UIAlertController!

接下來使用它,而無需再次定義,請按照以下步驟檢查它是否被取消:

if alert != nil {
// still presented 
}else {

// dismissed 
}

這很簡單,您可以在函數中使用UIAlertViewController的完成情況來檢查它是否已關閉並執行代碼。

將以下方法替換為我的方法:

let okayButton = UIAlertAction(title: okText, style: UIAlertActionStyle.Cancel, handler: nil)

代替:

當用戶按下“確定”按鈕時,計時器將啟動!

let okayButton = UIAlertAction(title: okText, style: UIAlertActionStyle.Cancel, handler: { (UIAlertAction) -> Void in

     //Start your second timer
     start2()
})

func Counting1刪除第二個計時器start2()

暫無
暫無

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

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