进度警报并未从Swift 3中消失

时间:2017-08-01 04:53:25

标签: ios swift3

我正在尝试创建一个在Swift 3中显示和解除ProgressDialog的函数。但在此代码中,对话框不会从视图控制器中消失。

func showLoadingDialog(show : Bool)  {
    if show {
        self.alert = UIAlertController(title: nil, message: "Please wait...", preferredStyle: .alert)
        let loadingIndicator = UIActivityIndicatorView(frame: CGRect(x: 10, y: 5, width: 50, height: 50))
        loadingIndicator.hidesWhenStopped = true
        loadingIndicator.activityIndicatorViewStyle = UIActivityIndicatorViewStyle.gray
        loadingIndicator.startAnimating()
        self.alert.view.addSubview(loadingIndicator)
        self.present(self.alert, animated: true, completion: nil)
    }else{
        self.dismiss(animated: false, completion: nil)
    }
}

我还尝试了以下方法来关闭此对话框,但它们都没有工作:

self.alert.view.removeFromSuperview()

self.alert.view.alpha = 0
self.presentingViewController?.dismiss(animated: true, completion: nil)

请帮帮我。如果你们有任何替代解决方案,请提出建议。

2 个答案:

答案 0 :(得分:4)

//试试这个

func showLoadingDialog(show : Bool)  {
        if show {
            if self.alert == nil{
                self.alert = UIAlertController(title: nil, message: "Please wait...", preferredStyle: .alert)
                let loadingIndicator = UIActivityIndicatorView(frame: CGRect(x: 10, y: 5, width: 50, height: 50))
                loadingIndicator.hidesWhenStopped = true
                loadingIndicator.activityIndicatorViewStyle = UIActivityIndicatorViewStyle.gray
                loadingIndicator.startAnimating()
                self.alert.view.addSubview(loadingIndicator)
            }
            if !self.alert.isBeingPresented {
                 self.present(self.alert, animated: true, completion: nil)
            }

        }else{
            self.alert.dismiss(animated: false, completion: nil)
        }
    }

答案 1 :(得分:3)

func showLoadingDialog

尝试使用

self.alert.dismiss(animated: false, completion: nil)

而不是

self.dismiss(animated: false, completion: nil)