Cant Get the activity indicator to show on iPhone app

前端 未结 2 2095
情深已故
情深已故 2021-01-17 02:04

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 th

相关标签:
2条回答
  • 2021-01-17 02:27

    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.
    0 讨论(0)
  • 2021-01-17 02:49

    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];
    }
    
    0 讨论(0)
提交回复
热议问题