2017-06-23 3 views
0

J'ai créé une superposition à exécuter pendant que j'exécute une saisie de données asynchrone sur le serveur afin que les utilisateurs ne continuent pas à appuyer sur des boutons dans l'interface utilisateur jusqu'à la saisie des données est fait. J'ai mis la fonction dans une classe singleton globale et je l'appelle en passant dans un bool pour dire si je veux montrer ou cacher. Je peux le montrer, mais je ne peux pas le cacher. Voici le code:ios 10+, Swift 3+ - Impossible de fermer UIAlertController à partir de l'instance Singleton

class DataModel { 
    static let sharedInstance = DataModel() 
    func accessNetworkData(vc: UIViewController, params: [String:Any], wsURLPath: String, completion: @escaping (_ response: AnyObject) ->()) { 
     DataModel.sharedInstance.toggleModalProgess(show: true) 
     // SHOW THE MODAL HERE ONCE THE DATA IS REQUESTED. 
     let url = URL(string: wsURLPath)! 
     let session = URLSession.shared 
     var request = URLRequest(url: url) 
     request.httpMethod = "POST" 
     do { request.httpBody = try JSONSerialization.data(withJSONObject: params, options: .prettyPrinted) } catch let error { print(error.localizedDescription) } 
     request.addValue("application/json", forHTTPHeaderField: "Content-Type") 
     request.addValue("application/json", forHTTPHeaderField: "Accept") 
     let task = session.dataTask(with: request as URLRequest, completionHandler: { data, response, error in 
      DataModel.sharedInstance.toggleModalProgess(show: false) 
      // NOW SINCE THE NETWORK ACTIVITY IS DONE, HIDE THE UIALERTCONTROLLER 
      guard error == nil else { 
       print("WEB SERVICE ERROR <----------------------------<<<<<< " + wsURLPath) 
       print(error!) 
       let resp: [String: String] = [ "conn": "failed" ] 

       DispatchQueue.main.async { completion(resp as NSDictionary) } 
       return 
      } 
      guard let data = data else { 
       print("WEB SERVICE ERROR <----------------------------<<<<<< " + wsURLPath) 
       return 
      } 
      do { 
       if let parsedJSON = try JSONSerialization.jsonObject(with: data, options: .mutableContainers) as? [String: Any] { 
        print("WEB SERVICE SUCCESS <----------------------------<<<<<< "+wsURLPath+" "+String(describing:params)) 
        if let parsedResponseDict = parsedJSON["d"] as? NSDictionary { 
         DispatchQueue.main.async { 
          completion(parsedResponseDict) 
         } 
        }else if let parsedResponseArr = parsedJSON["d"] as? NSArray { 
         DispatchQueue.main.async { 
          completion(parsedResponseArr) 
         } 
        }else { 
         print("INVALID KEY <----------------------------<<<<<< " + wsURLPath) 
         DispatchQueue.main.async { 
          completion(parsedJSON as AnyObject) 
         } 
        } 
       } 
      } catch let error { 
       print("Error with JSON Serialization") 
       print(error.localizedDescription) 
      } 
     }) 
     task.resume() 
    } 

Ici, je CONFIGURER LE UIALERTCONTROLLER

let modalAlert = UIAlertController(title: "Please Wait...", message: "Loading Data...", preferredStyle: UIAlertControllerStyle.alert) 
    let loadingIndicator = UIActivityIndicatorView(frame: CGRect(x: 10, y: 5, width: 50, height: 50)) 

    func toggleModalProgess(show: Bool) -> Void { 
     print("toggleModalProgess: show = " + String(describing: show)) 
     if (show) { 
      print("let's turn it on") 
      loadingIndicator.hidesWhenStopped = true 
      loadingIndicator.activityIndicatorViewStyle = UIActivityIndicatorViewStyle.gray 
      loadingIndicator.startAnimating() 
      modalAlert.view.addSubview(loadingIndicator) 
      modalAlert.show() 
     }else { 
      print("let's turn it off") 
      modalAlert.hide() 
     } 
    } 

    private init() { } 
} 

MAINTENANT LE PROLONGEMENT OU LA MAGIE SE PASSE

public extension UIAlertController { 
    func show() { 
     let win = UIWindow(frame: UIScreen.main.bounds) 
     let vc = UIViewController() 
     vc.view.backgroundColor = .clear 
     win.rootViewController = vc 
     win.windowLevel = UIWindowLevelAlert + 1 
     win.makeKeyAndVisible() 
     vc.present(self, animated: true, completion: nil) 
    } 
    func hide() { 
     // HERE IS WHERE I NEED TO HIDE IT BUT I AM HAVING ISSUES 
    } 
} 

Répondre

4

Afin de rejeter le UIAlertController (ce qui est une sous-classe de UIViewController), il devrait suffire d'appeler la méthode dismiss:

func hide() { 
    dismiss(animated: true, completion: nil) 
} 

Cela fonctionne très bien dans mon projet exemple.

0

Vous devriez faire ...

self.presentingViewController?.dismiss(animated: true, completion: nil) 

Hope it helps