2017-08-01 11 views
5

私は、Swift 3でProgressDialogを表示したり解除したりするための関数を作成しようとしていますが、このコードではダイアログはView Controllerから消えません。進捗アラートは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) 
    } 
} 

私はまた、このダイアログを閉じるために、次の方法を試してみましたが、それらのどれも働いた:

self.alert.view.removeFromSuperview() 

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

が私を助けてください。皆さんに他の解決策がある場合は、提案をしてください。

答えて

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) 
     } 
    } 
してみてくださいを使用してみてください
3
func showLoadingDialog

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

代わりの

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

+0

これは1回だけ働いていますが、私はこの機能をViewControllerで複数回呼び出しています。警報が却下されない2回目。私はネットワーク応答ブロックからこの関数を呼び出しています。 –

+0

あなたの質問を編集して、問題の詳細を理解できるように、実装方法に関するコードブロックを追加してください。ありがとう! – Kiester

関連する問題