2017-08-01 11 views
5

Próbuję utworzyć funkcję pokazywania i odrzucania ProgressDialog w Swift 3. Ale w tym kodzie okno dialogowe nie zwalnia z kontrolera widoku.Alert postępu nie zwalnia z widoku w Swift 3

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

Próbowałem również następujące metody odrzucić to okno, ale żaden z nich nie pracował:

self.alert.view.removeFromSuperview() 

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

Proszę mi pomóc. Jeśli masz jakieś alternatywne rozwiązanie, zrób sugestię.

Odpowiedz

4

// Spróbuj

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

W func showLoadingDialog,

spróbować użyć

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

zamiast

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

+0

to działa na jeden raz.Ale wywołuję tę funkcję wiele razy w moim ViewController. po raz drugi alert nie zwalnia. Wywołuję tę funkcję z bloku odpowiedzi sieci. –

+0

Edytuj pytanie i podaj więcej bloków kodu na temat jego implementacji, abyśmy mogli lepiej zrozumieć problem. Dzięki! – Kiester