关于uiscrollview:MBProgressBarHUD不会随Swift和iOS8一起出现

MBProgressBarHUD not appearing with swift and iOS8

我正在尝试使用MBProgressHUD(https://github.com/jdg/MBProgressHUD),并且能够使其与我的Podfile一起使用。 但是,现在没有出现平视显示。 有人能迅速成功地完成这项工作吗?

播客文件

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

MovieDetailViewController.swift

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
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)
    }

}


我有一个这样的功能:

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

...我在进行异步调用之前就调用了,然后在处理完响应后,我调用此函数将其隐藏:

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

还要确保将.h文件导入到桥接头文件中,如下所示:

1
#import"MBProgressHUD.h"


您缺少将HUD添加到Window或当前视图(self.view)的部分。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
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)
    }

}