快速,在同一视图中显示一个警报然后另一个警报

时间:2017-02-03 22:45:06

标签: ios swift

我在加载数据时尝试显示加载指示符:

let alert = UIAlertController(title: "Updating data", message: "Please wait...", preferredStyle: .alert)

            alert.view.tintColor = UIColor.black
            let loadingIndicator: UIActivityIndicatorView = UIActivityIndicatorView(frame: CGRect(x: 10,y: 5,width: 50, height: 50)) as UIActivityIndicatorView
            loadingIndicator.hidesWhenStopped = true
            loadingIndicator.activityIndicatorViewStyle = UIActivityIndicatorViewStyle.gray
            loadingIndicator.startAnimating();

            alert.view.addSubview(loadingIndicator)
            self.present(alert, animated: true, completion: nil)

然后,如果数据加载完成,我想显示另一个警告告诉用户数据已完成:

if dataLoaded{
    //dismiss the previous alert then show the new one
    dismiss(animated: false, completion: nil)
    let alert2 = UIAlertController(title: "Alert", message: "Data has been updated", preferredStyle: UIAlertControllerStyle.alert)
    alert2.addAction(UIAlertAction(title: "Ok", style: UIAlertActionStyle.default, handler: nil))
    self.present(alert2, animated: true, completion: nil)
}

第二个警报永远不会出现,任何想法。我对swift很新。

提前致谢 BT

1 个答案:

答案 0 :(得分:2)

使用完成块显示下一个警报。试试这个代码,这是有效的。

let alert = UIAlertController(title: "Updating data", message: "Please wait...", preferredStyle: .alert)

    alert.view.tintColor = UIColor.black
    let loadingIndicator: UIActivityIndicatorView = UIActivityIndicatorView(frame: CGRect(x: 10,y: 5,width: 50, height: 50)) as UIActivityIndicatorView
    loadingIndicator.hidesWhenStopped = true
    loadingIndicator.activityIndicatorViewStyle = UIActivityIndicatorViewStyle.gray
    loadingIndicator.startAnimating();

    alert.view.addSubview(loadingIndicator)

    self.present(alert, animated: true) {
        self.dismiss(animated: true, completion: { 
            let alert2 = UIAlertController(title: "Alert", message: "Data has been updated", preferredStyle: UIAlertControllerStyle.alert)
            alert2.addAction(UIAlertAction(title: "Ok", style: UIAlertActionStyle.default, handler: nil))
            self.present(alert2, animated: true, completion: nil)
        })
    }