2017-02-04 394 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

任何人都可以提供每个警报视图的解决方法,而不是所有的修复? –

相关问题