Cant Get the activity indicator to show on iPhone

2020-03-31 08:24发布

I have a view which is created in IB. inside it I have a scroll view created programmatically. Within the scroll view I connect to a web service and fetch the content and the image. I want to show an activity indicator while doing this. So I have:

- (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];

And right after the scrollview is added, I have:

[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]; 

And in loadimage I have:

- (void)loadImage {

    [activityIndicator startAnimating];

I've tried [self.view.window addSubview:activityIndicator];, [self->ScrollView addSubview:activityIndicator];, [self.view addSubview:activityIndicator];

but I just can't get the indicator to show. Anyone have any idea what might be wrong?

2条回答
一夜七次
2楼-- · 2020-03-31 08:42

You should do this in you 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];
}

Note: "performSelectorInBackground" will show you an activity indicator on the view and your loadScroll method will fetch all data from internet and do other work.

-(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];
}
查看更多
兄弟一词,经得起流年.
3楼-- · 2020-03-31 08:48

What I have found so far after multiple times facing above problem with activity indicator.

  • Activity indicator and the function (or method or piece of code) that is required to be executed after appearance of activity indicator both can't run on main thread. If it is so activity indicator will not appear. The solution is to run activity indicator on main thread and the function in a background thread.
  • I have also faced a case in which the function followed by activity indicator can't run in a background thread. In my case it was playing a music file using MPMoviePlayerController that can't be executed in a background thread. What I did to solve this issue was to run activity indicator in the main thread and then used an NSTimer to call my method after a delay that was good enough for activity indicator to appear and start animating.
  • I have also found that activity indicator must be called in performSelectorOnMainThread and should be added as a subview on the top view of given viewController once it starts animating. Once it is not needed it should be removed by calling removeFromSuperView method.
查看更多
登录 后发表回答