2017-02-04 397 views
1

當在此UIAlertController上按下按鈕時,它會自動消除動畫。我可以關閉動畫嗎?iOS,Swift,UIAlertController,UIAlertAction - 如何在按下按鈕時關閉關閉動畫?

我試過呈現動畫:假,但仍然與動畫解散。

 func showOKMessage(title: String, message : String) { 
     self.alertController = UIAlertController(title: title, message: message, preferredStyle: .alert) 
     let okAction = UIAlertAction(title: "OK", style: .default) 
     self.alertController.addAction(okAction) 
     self.present(self.alertController, animated: true) 
    } 
+1

可以添加代碼,請 – Ram

+0

代碼請sims像一個簡單的修復 –

+0

在simliar情況下,我發現禁用該按鈕是一個很好的解決方法,我不知道這將有助於你的情況沒有更多的信息,雖然。 –

回答

1

首先是我的嘗試是,創建一個UIAlertController的參考來處理在dismiss(animated:completion:)動畫設置爲false在處理程序的UIAlertAction的(這將按下確定按鈕後要執行的代碼) :

import UIKit 

class ViewController: UIViewController { 

    var alert: UIAlertController! 

    @IBAction func alertViewButtonPressed(_ sender: UIButton) { 
    alert = UIAlertController(title: "", message: "Hello", preferredStyle: .alert) 
    let action = UIAlertAction(title: "OK", style: .default) { _ in 
     // this code executes after you hit the OK button 
     self.alert.dismiss(animated: false, completion: nil) 
    } 
    alert.addAction(action) 
    self.present(alert, animated: true) 
    } 
} 

不幸的是,動畫仍然存在:

enter image description here

對我而言,唯一的辦法就是overridedismiss(animated:completion:)方法,並將super調用中的動畫標誌設置爲false。您也不需要向處理程序添加代碼,也沒有理由爲該解決方案創建參考。 (注:現在每個呈現視圖控制器獲取該視圖控制器沒有動畫駁回):

import UIKit 

class ViewController: UIViewController { 

    @IBAction func alertViewButtonPressed(_ sender: UIButton) { 
    let alert = UIAlertController(title: "", message: "Hello", preferredStyle: .alert) 
    let action = UIAlertAction(title: "OK", style: .default, handler: nil) 
    alert.addAction(action) 
    self.present(alert, animated: true) 
    } 

    override func dismiss(animated flag: Bool, completion: (() -> Void)? = nil) { 
    // view controller which was presented modally by the view controller gets dismissed now without animation 
    super.dismiss(animated: false, completion: completion) 
    } 
} 

現在警報視圖被解僱而無需動畫:

enter image description here

+0

任何人都可以提供每個警報視圖的解決方法,而不是所有的修復? –

相關問題