Swift从另一个类添加子视图

时间:2018-07-11 13:00:39

标签: swift

我为我的请求逻辑创建了一个类,可以在需要时调用它。

我想显示一个“正在加载”子视图,该视图将在http请求发生时显示。任务完成后,它将被删除。我的问题是,子视图仅在功能完成后才出现,而不会在功能启动后立即出现。

我感觉是因为我正在将函数分配给变量“结果”。

如何在功能启动后立即显示子视图?

let result = serverCall().post(endPoint: "registermanual.php?", params: "\(params)", fromViewController: self)

服务器呼叫类别:

class serverCall {

    func post(endPoint: String, params: String, fromViewController: UIViewController) -> [[String: AnyObject]]{

        print("doing")
        DispatchQueue.main.async {
            let loadingScreen = xibLoad().loadingScreen()
            loadingScreen.frame.size.height = fromViewController.view.frame.height
            fromViewController.view.addSubview(loadingScreen)
            fromViewController.view.layoutSubviews()
        }

        let group = DispatchGroup()
        group.enter()

        var result = [[String:AnyObject]]()
        var errors = false
        let url = URL(string: "\(server)\(endPoint)")
        print(url!)
        var request = URLRequest(url: url!)
        request.setValue("application/x-www-form-urlencoded", forHTTPHeaderField: "Content-Type")
        request.httpMethod = "POST"
        request.httpBody = params.data(using: .utf8)
        let urlconfig = URLSessionConfiguration.default
        urlconfig.timeoutIntervalForRequest = 5
        urlconfig.timeoutIntervalForResource = 5
        let session = URLSession(configuration: urlconfig, delegate: self as? URLSessionDelegate, delegateQueue: nil)
        let task = session.dataTask(with: request) { data, response, error in

            if let error = error as? NSError {
                if error.code == NSURLErrorCancelled {
                    print("boo")
                    errors = true
                } else {
                    print("boo")
                    errors = true
                }

                group.leave()
               return
            }
            guard let data = data, error == nil else {

                errors = true
                group.leave()
                return
            }

            if let httpStatus = response as? HTTPURLResponse, httpStatus.statusCode != 200, httpStatus.statusCode != 401 {
                print("statusCode should be 200, but is \(httpStatus.statusCode)")
                print("response = \(response!)")
                errors = true
                group.leave()
                return

            }
                do {
                    if let jsonResult = try JSONSerialization.jsonObject(with: data, options: []) as? [[String:AnyObject]] {
                        result = jsonResult
                        print(result)
                        group.leave()
                    }else{
                        errors = true
                        group.leave()
                        return
                    }
                } catch { print("JSON serialization failed")

                    group.leave()
            }
        }
        task.resume()

        group.wait()


        if(errors == true){
            let alert = UIAlertController(title: noInternetTitle, message: noInternetMessage, preferredStyle: UIAlertControllerStyle.alert)
            alert.addAction(UIAlertAction(title: "Ok", style: UIAlertActionStyle.default, handler: nil))
            fromViewController.present(alert, animated: true, completion: nil)

        }
        return result
    }

1 个答案:

答案 0 :(得分:0)

是的,因为您正在等待结果。 您应该使用完成块而不是返回值。

func post(端点:字符串,参数:字符串,fromViewController:UIViewController,完成:((_结果:[[String:AnyObject]]->无效)?)