【问题标题】:ActivityIndicator not showingActivityIndi​​cator 不显示
【发布时间】:2014-01-03 16:20:08
【问题描述】:

我正在实现似乎是 iOS 活动指示器的标准代码,但它没有显示。这就是我所做的,遵循这里的建议:

viewDidLoad:

indicator = [[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
indicator.frame = CGRectMake(0, 0, 80.0, 80.0);
indicator.hidden = NO;
indicator.center = self.view.center;
[self.view insertSubview:indicator atIndex:100]; // to be on the safe side
[indicator bringSubviewToFront:self.view];
[UIApplication sharedApplication].networkActivityIndicatorVisible = TRUE;

然后在viewWillAppear,就在调用我的长流程方法之前:

[NSThread detachNewThreadSelector:@selector(threadStartAnimating:) toTarget:self withObject:nil];

我的线程开始动画:

-(void)threadStartAnimating:(id)data
{
    [indicator startAnimating];
}

然后我有一个 Parse.com 方法在后台做一些工作:

[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {       
    if(!error) {
        // do some work
        [indicator stopAnimating];
    }
}

据我所知,这应该可行。但事实并非如此。我错过了什么?

【问题讨论】:

  • UIKit 只能从主线程调用
  • 只在主线程上更新 UI
  • [indicator bringSubviewToFront:self.view]; 你的意思可能是[self.view bringSubviewToFront:indicator];
  • 谢谢,@Jkmn,我改变了它,但仍然没有运气。我放入了几个indicator.isAnimating 日志,一旦我启动它就会动画,在数据库获取期间持续5-7 秒,然后停止。但在屏幕上 - nada。
  • 我想我应该补充一点,我使用的是 xCode 5,在最新 OS7 的 iPhone 4S 上运行该应用程序。

标签: ios activity-indicator


【解决方案1】:

当您想要操作视图(即 addSubview 等)时,最好在 viewWillAppear 中进行。 viewDidLoad 操作视图还为时过早,因为其他方法仍会改变它(即 setFrame)。

因此,将您当前的代码从 viewDidLoad 移动到 viewWillAppear 应该可以解决问题!

【讨论】:

    【解决方案2】:

    startAnimating 应该在主线程而不是新线程中调用。 所以只需在viewWillAppear 中调用[indicator startAnimating];

    【讨论】:

    • 仍然没有指标出现。我什至尝试注释掉 [indicator stopAnimating] 但什么也没做。我的指标声明正确吗?
    • 您能否发布indicator 的声明,也带有断点,并确保indicator 不是nil
    • 声明在我的问题中。我做了一个 NSLog,指标不为零。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-05
    • 1970-01-01
    • 2016-11-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多