2012-01-04 37 views
0

我有一个在IB中创建的视图。它里面我有一个编程创建的滚动视图。在滚动视图中,我连接到Web服务并获取内容和图像。我想在做这个时显示一个活动指标。所以,我有:增加了滚动无法获取要在iPhone应用上显示的活动指示器

并经过
- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    activityIndicator = [[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray]; 
    activityIndicator.frame = CGRectMake(0.0, 0.0, 40.0, 40.0); 
    activityIndicator.center = self.view.center; 
    [self.view.window addSubview:activityIndicator]; 

权利,我有:

[self.view addSubview:scrollView]; 
// ADD Scroll View Ends 


    //Add the Lisitng Image 
    NSOperationQueue *queue = [NSOperationQueue new]; 
    NSInvocationOperation *operation = [[NSInvocationOperation alloc] 
             initWithTarget:self 
             selector:@selector(loadImage) 
             object:nil]; 
    [queue addOperation:operation]; 

而且在我的LoadImage有:

- (void)loadImage { 

    [activityIndicator startAnimating]; 

我已经试过[self.view .window addSubview:activityIndi​​cator],; [self-> ScrollView addSubview:activityIndi​​cator],; [self.view addSubview:activityIndi​​cator];

但我只是不能让指标显示。任何人都知道什么可能是错的?

+0

在这里发布您的整个.m类。这是因为您添加该活动指示符的顺序。 – 2012-01-04 11:00:15

+0

是的,Ankit可能在这里的正确轨道上。你的UIScrollView是否有透明背景?如果没有,那么你实际上是掩盖了UIActivityIndi​​catorView。你可以随时在[self.view addSubView:scrollView]之后;调用[self.view bringSubviewToFront:activityIndi​​cator]; – 2012-01-04 11:21:01

回答

4

你应该这样做viewDidLoad中

- (void)viewDidLoad 
{ 
    //Start Activity Indicator View 
    indicatorView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge]; 
    indicatorView.frame = CGRectMake(40.0, 20.0, 60.0, 60.0); 
    indicatorView.center = self.view.center; 
    [self.view addSubview:indicatorView]; 
    [UIApplication sharedApplication].networkActivityIndicatorVisible = YES; 
    [indicatorView startAnimating]; 

    [self performSelectorInBackground:@selector(loadscroll) withObject:self]; 
} 

注:“performSelectorInBackground”会告诉你在查看活动指标和你loadScroll方法将获取从互联网上的所有数据和做其他工作。

-(void)loadscroll 
{ 
    //Your Scroll View 
     //Your other Data Manipulation even from internet 
     //When data is fetched display it 
     [self removeActivityIndicator]; //To stop activity Indicator  
} 

- (void)removeActivityIndicator 
{ 
     [indicatorView stopAnimating]; 
} 
+0

哈立德。我试过并实施了这个,但似乎没有解决我的问题。我注意到的一件事是,当应用程序从后台恢复时,我可以看到指示器。我已经按照您的建议将所有代码放入loadscroll中。但活动指标仍然隐形 – 2012-01-04 12:23:31

+0

Savas Zorlu!它的工作,我先确认它,然后发布答案。你可以发送我的.m文件。我的电子邮件ID是[email protected]。 – 2012-01-04 13:07:20

+0

谢谢哈立德。我已将它发送到您的电子邮件 – 2012-01-04 18:44:43

0

我多次面对活动指标上面的问题后发现的。

  • 活动指示灯和活动指示灯出现后需要执行的功能(或方法或代码)无法在主线程上运行。如果是这样,活动指示器将不会出现。解决方案是在主线程上运行活动指示器,并在后台线程中运行该功能。
  • 我也遇到了一个情况,其中活动指示器后面的函数不能在后台线程中运行。在我的情况下,它正在播放使用MPMoviePlayerController的音乐文件,无法在后台线程中执行。我所做的解决此问题的方法是在主线程中运行活动指示器,然后使用NSTimer在延迟后调用我的方法,该时间足以使活动指示器出现并开始动画。
  • 我还发现活动指示器必须在performSelectorOnMainThread中调用,并且应该在给定的viewController的顶视图上添加一个子视图,一旦它开始动画。 一旦不需要,它应该通过调用removeFromSuperView方法来删除。