簡體   English   中英

以編程方式迅速消除警報

[英]Dismiss Alert Programmatically Swift

我需要UIAlert的幫助。 我收到一個錯誤"UIAlertController can only have one action with a style of UIAlertActionStyleCancel" 我知道這是因為我正在函數外部啟動此警報,但是不確定如何修復它。 如果blah < 80 {有條件,如何在警報中訪問警報?

let alertView = UIAlertController(title: "Blah", message: "", preferredStyle: UIAlertControllerStyle.Alert)
@IBAction func blahButtonPressed(sender: AnyObject) {        

        alertView.addAction(UIAlertAction(title: "Do Something", style: .Default, handler: { (action: UIAlertAction!) in
            // I have code here
        }))

        alertView.addAction(UIAlertAction(title: "Do Something 2", style: .Default, handler: { (action: UIAlertAction!) in
            // I have code here
        }))

        alertView.addAction(UIAlertAction(title: "Cancel", style: .Cancel, handler: { (action: UIAlertAction!) in
            // I have code here
        }))

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

}

在代碼的后面,我通過藍牙獲取值,並且如果值低於80,則需要關閉警報。

if blah < 80 {
alertView.dismissViewControllerAnimated(true, completion: nil)
}

我不是100%,但是當您只按一次按鈕時它是否起作用? 如果是這樣,則可能是因為您alertView動作添加到alertView@IBAction 相反,你可能會想嘗試移動添加的UIAlertAction的在外面@IBAction ,只呈現它里面的警報視圖。 像這樣:

let alertView = UIAlertController(title: "Blah", message: "", preferredStyle: UIAlertControllerStyle.Alert)
alertView.addAction(UIAlertAction(title: "Do Something", style: .Default, handler: { (action: UIAlertAction!) in
        // I have code here
    }))

alertView.addAction(UIAlertAction(title: "Do Something 2", style: .Default, handler: { (action: UIAlertAction!) in
        // I have code here
    }))

alertView.addAction(UIAlertAction(title: "Cancel", style: .Cancel, handler: { (action: UIAlertAction!) in
        // I have code here
    }))

@IBAction func blahButtonPressed(sender: AnyObject) {
  presentViewController(alertView, animated: true, completion: nil)
}

這樣, UIAlertAction的沒有得到添加了‘blahButton’被按下,每一次(這將導致多個UIAlertAction與‘UIAlertActionStyleCancel’的風格)

如果有人遇到這個問題,這就是我的解決方法。

var newAlert: AnyObject?


@IBAction func blahButtonPressed(sender: AnyObject) {        
    let alertView = UIAlertController(title: "Blah", message: "", preferredStyle: UIAlertControllerStyle.Alert)
    alertView.addAction(UIAlertAction(title: "Do Something", style: .Default, handler: { (action: UIAlertAction!) in
        // I have code here
    }))

    alertView.addAction(UIAlertAction(title: "Do Something 2", style: .Default, handler: { (action: UIAlertAction!) in
        // I have code here
    }))

    alertView.addAction(UIAlertAction(title: "Cancel", style: .Cancel, handler: { (action: UIAlertAction!) in
        // I have code here
    }))

   newAlert = alertView

        presentViewController(alertView, animated: true, completion: nil) 
}

//當我需要關閉時。 如果blah低於80

if blah < 80 {
    if let newAlertView = newAlert as? UIAlertController {
       newAlertView.dismissViewControllerAnimated(true, completion: nil)
    }
}

暫無
暫無

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

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