Question

I am having some trouble getting my UIActivityIndicatorView to start animating. Here is my setup:

In my viewDidLoad in my view controller I have:

- (void)viewDidLoad{
    schoolList = NO;
   _activityIndicator = [[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
   [_activityIndicator startAnimating];

   [NSThread detachNewThreadSelector: @selector(getSchoolList) toTarget: self withObject: nil];
   [self performSelector:@selector(updateUI) withObject:nil afterDelay:20.0];
   [super viewDidLoad];

 }

The selector getSchoolList communicates with a server to retrieve a list of schools in a given state. Then, the selector updateUI is called to populate my UIPickerView with the list. In my updateUI selector I have:

-(void)updateUI {   
  _schools = [_server returnData];

  if(!(_schools == nil)) {
    NSLog(@"update the UI");
   }
  else
    NSLog(@"Error:Show re-load button");

[_activityIndicator stopAnimating];
}

When I run this code, my UIActivityIndicatorView shows up, but does not animate. Can someone explain the proper way to animate my UIActivityIndicatorView? Any help is much appreciated.

Was it helpful?

Solution

You need to add the UIActivityIndicatorView to your view in viewDidLoad like this:

- (void)viewDidLoad {
    schoolList = NO;
   _activityIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];

   [self addSubview:_activityIndicator];

   [_activityIndicator startAnimating];

   [NSThread detachNewThreadSelector: @selector(getSchoolList) toTarget: self withObject: nil];
   [self performSelector:@selector(updateUI) withObject:nil afterDelay:20.0];
   [super viewDidLoad];
}

EDIT

If _activityIndicator is a properly connected IBOutlet to a UIActivityIndicatorView, you should only need to check the 'animating' box. There would be no need to alloc/init another UIActivityIndicatorView.

OTHER TIPS

Breakpoint the update function, but I don't see where you add that as a view to the hierarchy. I think you're looking at a different indicator view in the program.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top