Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/113.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 隐藏添加到视图中的HUD的最佳做法_Ios_Xcode_Swift_Xcode6_Mbprogresshud - Fatal编程技术网

Ios 隐藏添加到视图中的HUD的最佳做法

Ios 隐藏添加到视图中的HUD的最佳做法,ios,xcode,swift,xcode6,mbprogresshud,Ios,Xcode,Swift,Xcode6,Mbprogresshud,假设我有以下代码: @IBAction func signInButtonPressed(sender: AnyObject) { MBProgressHUD.showHUDAddedTo(self.view, animated: true) if let url = NSURL(string: someURL) { // ... let task = NSURLSession.sharedSession().dataTask

假设我有以下代码:

@IBAction func signInButtonPressed(sender: AnyObject) {
    MBProgressHUD.showHUDAddedTo(self.view, animated: true)

    if let url = NSURL(string: someURL) {

            // ...

            let task = NSURLSession.sharedSession().dataTaskWithRequest(request) {
                (data, response, error) in

                if let httpError = error {
                    dispatch_async(dispatch_get_main_queue()) {
                        self.alert("Error", message: "Unable to sign in: \(httpError.localizedDescription)")
                    }
                    return
                }

                var deserializationError: NSError?
                if let jsonData = NSJSONSerialization.JSONObjectWithData(data, options: nil, error: &deserializationError) as? [String: AnyObject] {

                    // ...

                    if let error = customer.error {
                        dispatch_async(dispatch_get_main_queue()) {
                            self.alert("Error", message: error)
                        }
                    } else {
                        // Show other view controller
                    }
                } else {
                    if let unwrappedError = deserializationError {
                        dispatch_async(dispatch_get_main_queue()) {
                            self.alert("Error", message: "Unable to sign in: \(deserializationError)")
                        }
                    }
                }
            }
            task.resume()
        } else {
            if let unwrappedError = serializationError {
                self.alert("Error", message: "Unable to sign in: \(serializationError)")
            }
        }
    }
}
隐藏添加到
self.view
中的HUD的正确方法是什么?有没有比添加

dispatch_async(dispatch_get_main_queue()) {
    MBProgressHUD.hideHUDForView(self.view, animated: true)
    return
}
对每个
if
else
分支进行编码


请提前感谢。

在url初始化后和任务开始前,首先显示hud

if let url = NSURL(string: someURL) {
    MBProgressHUD.showHUDAddedTo(self.view, animated: true)
    // start the request here
然后在回调块启动后立即隐藏它

let task = NSURLSession.sharedSession().dataTaskWithRequest(request) {
                (data, response, error) in
    dispatch_async(dispatch_get_main_queue()) {
        MBProgressHUD.hideHUDForView(self.view, animated: true)
    }
    // here goes other logic
在hud隐藏后,您不必调用
return