首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >UIActivityIndicatorView没有出现

UIActivityIndicatorView没有出现
EN

Stack Overflow用户
提问于 2015-10-20 22:12:48
回答 2查看 4.3K关注 0票数 2

我正在尝试实现一个在用户处于应用程序购买过程中运行的UIActivityIndicatorView。由于某些原因,即使我已经创建了视图的子视图,UIActivityIndicatorView也不会出现。

代码语言:javascript
复制
class RemoveAdsViewController: UIViewController {

@IBAction func btnAdRemoval(sender: UIButton) {
    let buyProgress = UIActivityIndicatorView(activityIndicatorStyle: .White)
    buyProgress.center = self.view.center
    self.view.addSubview(buyProgress)
    buyProgress.startAnimating()
    print(buyProgress)
    PFPurchase.buyProduct("", block: { (error:NSError?) -> Void in
        if error != nil{
            let alert = UIAlertController(title: "Error", message: error?.localizedDescription, preferredStyle: UIAlertControllerStyle.Alert)

            alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler: nil))

            self.presentViewController(alert, animated: true, completion: nil)
        }
    })
    buyProgress.stopAnimating()
    buyProgress.removeFromSuperview()
}

PFRestore:

代码语言:javascript
复制
restoreProgress.startAnimating()
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), {
       PFPurchase.restore()
       dispatch_async(dispatch_get_main_queue(), {
           restoreProgress.stopAnimating()
       })
 })
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2015-10-20 22:25:32

再看一看,问题很简单。停止并移除活动指示器太快了。您需要停止并在完成块中删除它。

代码语言:javascript
复制
@IBAction func btnAdRemoval(sender: UIButton) {
    let buyProgress = UIActivityIndicatorView(activityIndicatorStyle: .White)
    buyProgress.center = self.view.center
    self.view.addSubview(buyProgress)
    buyProgress.startAnimating()
    print(buyProgress)
    PFPurchase.buyProduct("", block: { (error:NSError?) -> Void in
        buyProgress.stopAnimating()
        buyProgress.removeFromSuperview()

        if error != nil{
            let alert = UIAlertController(title: "Error", message: error?.localizedDescription, preferredStyle: UIAlertControllerStyle.Alert)

            alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler: nil))

            self.presentViewController(alert, animated: true, completion: nil)
        }
    })
}

还需要确保完成块的内容是在主线程上完成的。

票数 1
EN

Stack Overflow用户

发布于 2015-10-20 22:28:24

问题是你这么做

代码语言:javascript
复制
buyProgress.startAnimating()

紧随其后的是

代码语言:javascript
复制
buyProgress.stopAnimating()

因为PFPurchase.buyProduct是一个异步调用,它将立即返回,而您没有看到您的活动指示符是在一个运行循环循环中发生的。

你得走了

代码语言:javascript
复制
buyProgress.stopAnimating()

在封闭里面,就像这样

代码语言:javascript
复制
PFPurchase.buyProduct("", block: { (error:NSError?) -> Void in
            if error != nil{
                let alert = UIAlertController(title: "Error", message: error?.localizedDescription, preferredStyle: UIAlertControllerStyle.Alert)
                buyProgress.stopAnimating()

                alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler: nil))

                self.presentViewController(alert, animated: true, completion: nil)
            }
        })
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/33247724

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档