5

I'm trying to create a function for showing and dismissing a ProgressDialog in Swift 3. But in this code the dialog is not dismissing from the view controller.

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)
    }
}

I have also tried the following methods for dismiss this dialog, but none of them worked:

self.alert.view.removeFromSuperview()

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

Please help me. If you guys have any alternate solution, please make a suggestion.

Vinayak B
  • 4,430
  • 4
  • 28
  • 58

2 Answers2

4

//Try this

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)
        }
    }
Berlin Raj
  • 124
  • 6
3

In func showLoadingDialog,

try to use

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

instead of

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

Kiester
  • 147
  • 1
  • 7
  • it is working for one time.But I am calling this function multiple time in my ViewController. the second time the alert not dismissing. I am calling this function from network response block. – Vinayak B Aug 01 '17 at 05:26
  • Please edit your question and provide more code blocks on how it is implemented so that we can understand more about the issue. Thanks! – Kiester Aug 01 '17 at 05:31