2014-09-26 48 views
3

我试图使用MBProgressHUD(https://github.com/jdg/MBProgressHUD),并能够得到它与我的Podfile一起工作。但是,现在没有出现hud。有没有人能够成功地使用swift进行工作?MBProgressBarHUD没有出现与迅速和iOS8

Podfile

platform :ios, '8.0' 
pod 'AFNetworking' 
pod 'MBProgressHUD', '~> 0.8' 

MovieDetailViewController.swift

override func viewDidLoad() { 
    super.viewDidLoad() // Do any additional setup after loading the view. 
    var url: String = "http://api.rottentomatoes.com/api/public/v1.0/movies/"+movieID!+".json?apikey="+apiKey 
    NSLog("url = \(url)") 
    var request = NSURLRequest(URL: NSURL(string: url)) 

    // setup HUD; https://github.com/jdg/MBProgressHUD 
    var hud = MBProgressHUD() 
    hud.show(true) 

    NSURLConnection.sendAsynchronousRequest(request, queue: NSOperationQueue.mainQueue())  
    { 
     (response: NSURLResponse!, data: NSData!, error: NSError!) -> Void in 

     var err: NSError? 
     var object = NSJSONSerialization.JSONObjectWithData(data, options: nil, error: &err) as NSDictionary 

     if let yearInt = object["year"] as? Int { 
      self.year.text = String(yearInt) 
     } 
     self.title = object["title"] as? String 
     self.movieTitle.text = self.title 

     self.synopsis.text = object["synopsis"] as? String 
     self.mpaaRating.text = object["mpaa_rating"] as? String 
     var ratings = object["ratings"] as NSDictionary 
     var posters = object["posters"] as NSDictionary 

     // setup background picture 
     var posterUrl = posters["thumbnail"] as String 
     var image = UIImageView() 
     image.setImageWithURL(NSURL(string: posterUrl)) 
     image.contentMode = UIViewContentMode.ScaleAspectFill 
     self.scrollView.backgroundColor = UIColor.clearColor() 
     self.scrollView.addSubview(image) 

     // stop the hud 
     hud.hide(true) 
    } 

} 
+0

您是如何将MBProgressHUD首先导入您的项目的?我试图在我的项目中使用它,但似乎我甚至不能像'code'#import“MBProgressHUD.h”'code'“那样导入它。相反,我使用'代码'#导入“MBProgressHUD/MBProgressHUD.h”'代码',但我仍然不能在我的项目中使用它。我用'use_framworks!'在cocoapods,但不应该导致任何问题吗? – 2015-06-10 02:01:39

回答

3

你缺少在其中添加了HUD到Window或当前视图(self.view)的一部分。

override func viewDidLoad() { 
    super.viewDidLoad() // Do any additional setup after loading the view. 
    var url: String = "http://api.rottentomatoes.com/api/public/v1.0/movies/"+movieID!+".json?apikey="+apiKey 
    NSLog("url = \(url)") 
    var request = NSURLRequest(URL: NSURL(string: url)) 

    // setup HUD; https://github.com/jdg/MBProgressHUD 
    var hud = MBProgressHUD.showHUDAddedTo(self.view, animated: true) 

    NSURLConnection.sendAsynchronousRequest(request, queue: NSOperationQueue.mainQueue())  
    { 
     (response: NSURLResponse!, data: NSData!, error: NSError!) -> Void in 

     var err: NSError? 
     var object = NSJSONSerialization.JSONObjectWithData(data, options: nil, error: &err) as NSDictionary 

     if let yearInt = object["year"] as? Int { 
      self.year.text = String(yearInt) 
     } 
     self.title = object["title"] as? String 
     self.movieTitle.text = self.title 

     self.synopsis.text = object["synopsis"] as? String 
     self.mpaaRating.text = object["mpaa_rating"] as? String 
     var ratings = object["ratings"] as NSDictionary 
     var posters = object["posters"] as NSDictionary 

     // setup background picture 
     var posterUrl = posters["thumbnail"] as String 
     var image = UIImageView() 
     image.setImageWithURL(NSURL(string: posterUrl)) 
     image.contentMode = UIViewContentMode.ScaleAspectFill 
     self.scrollView.backgroundColor = UIColor.clearColor() 
     self.scrollView.addSubview(image) 

     // stop the hud 
     MBProgressHUD.hideAllHUDsForView(self.view, animated: true) // Or just call hud.hide(true) 
    } 

} 
3

我有这样的功能:

func showLoadingSpinner() { 
    let loading = MBProgressHUD.showHUDAddedTo(self.view, animated: true) 
    loading.mode = MBProgressHUDModeDeterminate 
    loading.labelText = "Loading..."; 
} 

...我叫只是在做异步调用之前,然后当我做处理响应,我调用这个函数来隐藏它:

MBProgressHUD.hideHUDForView(self.view, animated: true) 

另外,还要确保您导入您的桥接头文件的.h文件中,就像这样:

#import "MBProgressHUD.h" 
+0

我用cocoapods,但似乎我不能像'code'#import“MBProgressHUD.h”'code'那样导入 - 编译不会通过。但'code'#import“MBProgressHUD/MBProgressHUD.h”'code'与编译一起工作,但我仍然没有在项目中使用它 - 如果我使用MBProgressHUD(),则无法编译。任何帮助? – 2015-06-10 01:33:53