ios – 如何在服务器未返回数据时停止MBProgressHUD并添加子视图

前端之家收集整理的这篇文章主要介绍了ios – 如何在服务器未返回数据时停止MBProgressHUD并添加子视图前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
在我的应用程序中,我有这个类从我的服务器获取数据:
class Api{

func loadOffers(completion:(([Offers])-> Void),offer_id: String,offerStatus:String){


    let myUrl = NSURL(string: "http://www.myServer.PHP/api/v1.0/offers.PHP")
    let request = NSMutableURLRequest(URL: myUrl!)
    request.HTTPMethod = "POST"
    let postString = "offer_id=\(offer_id)&offerStatus=\(dealStatus)&action=show"
    request.HTTPBody = postString.dataUsingEncoding(NSUTF8StringEncoding,allowLossyConversion: true)

    let task = NSURLSession.sharedSession().dataTaskWithRequest(request)
        { data,response,error in              
            if error != nil {                  
                println("error\(error)")                 
            }else{   
               var err:NSError?

               let jsonObject : AnyObject! = NSJSONSerialization.JSONObjectWithData(data,options: NSJSONReadingOptions.MutableContainers,error: nil)

                if let dict = jsonObject as? [String: AnyObject] {

                if let myOffers = dict["offers"] as? [AnyObject] {

                var offers = [Offers]()

                for offer in myOffers{

                let offer = Offers(dictionary: offer as! NSDictionary)

                offers.append(offer)                       

                let priority = DISPATCH_QUEUE_PRIORITY_DEFAULT
                                    dispatch_async(dispatch_get_global_queue(priority,0 )){
                                                                                   dispatch_async(dispatch_get_main_queue()){

                completion(offers)

                             }         
                           }                                
                        }
                    }
                }                
            }            
      }
    task.resume()       
   }
 }

然后在我的View Controller中加载模型:

class MyViewController: UIViewController,UITableViewDataSource,UITableViewDelegate {


    var offers: [Offers]!

    func loadModel() {
    let loadingNotification = MBProgressHUD.showHUDAddedTo(self.view,animated: true)
    loadingNotification.mode = MBProgressHUDMode.Indeterminate
    loadingNotification.labelText = "updating your offers..."
    offers = [Offers]()
    let api = Api()
    api.loadOffers(didLoadOffers,offer_id: dealIdent!,offerStatus: "open")

    }

    func didLoadOffers(offers:[Offers]){

    self.offers = offers
    self.tableView.reloadData()
    MBProgressHUD.hideAllHUDsForView(self.view,animated: true)
    self.refreshControl.endRefreshing()
    }

    override func viewWillAppear(animated: Bool) {

    loadModel()

    }
 }

除了当JSON字典为空时,一切都有效,这意味着没有提供MBProgressHUD保持旋转.

我想停止活动指示器添加一个子视图,而不是说没有优惠.任何建议将不胜感激.

我试过了:

if offers.isEmpty {
MBProgressHUD.hideAllHUDsForView(self.view,animated:true)
}

并且

if offers == 0 {
MBProgressHUD.hideAllHUDsForView(self.view,animated:true)
}

但它不起作用

谢谢

解决方法

发生这种情况是因为您在主队列中设置了HUD,但正试图从另一个队列中删除.所有与UI相关的更改都应该在main_queue()中完成

尝试使用此代码

dispatch_async(dispatch_get_main_queue(),{ 

  // your code to modify HUD here

});

猜你在找的iOS相关文章