2017-08-01 14 views
5

Tôi đang cố tạo một hàm để hiển thị và loại bỏ ProgressDialog trong Swift 3. Nhưng trong đoạn mã này hộp thoại không được loại bỏ khỏi bộ điều khiển xem.Cảnh báo tiến trình không loại bỏ chế độ xem trong 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) 
    } 
} 

Tôi cũng đã thử các phương pháp sau đây để loại bỏ hộp thoại này, nhưng không ai trong số họ làm việc:

self.alert.view.removeFromSuperview() 

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

Xin hãy giúp tôi. Nếu các bạn có bất kỳ giải pháp thay thế nào, vui lòng đưa ra đề xuất.

Trả lời

4

// Hãy thử điều này

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

Trong func showLoadingDialog,

cố gắng sử dụng

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

thay vì

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

+0

nó hoạt động trong một thời gian. Nhưng tôi gọi hàm này nhiều lần trong ViewController của tôi. lần thứ hai cảnh báo không loại bỏ. Tôi đang gọi chức năng này từ khối phản hồi mạng. –

+0

Vui lòng chỉnh sửa câu hỏi của bạn và cung cấp thêm các khối mã về cách nó được triển khai để chúng tôi có thể hiểu thêm về vấn đề này. Cảm ơn! – Kiester

Các vấn đề liên quan