0

I am trying to use the activity indicator in iOS and not able to. I followed a thread on Stackoverflow and using it like that. This is what i wrote:

-(void)viewDidLoad
{
    [NSThread detachNewThreadSelector:@selector(threadStartAnimating:) toTarget:self withObject:self];

    UITapGestureRecognizer *tGR = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(doThisForTap:)];
    tGR.numberOfTapsRequired = 1;

    [myRollTypeLabel addGestureRecognizer:tGR];
    myRollTypeLabel.userInteractionEnabled = YES;

    [self.scrollView addSubview:myRollTypeLabel];
}

- (void) threadStartAnimating:(id)data
 {
self.activityIndicatorNew =[[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
self.activityIndicatorNew.color = [UIColor redColor];
[self.activityIndicatorNew startAnimating];
 }
- (void)doThisForTap :(UIGestureRecognizer *)gest
{
  //lots of computation.
}
- (void)viewWillDisappear:(BOOL)animated
{
[self.activityIndicatorNew stopAnimating];
self.activityIndicatorNew.hidden = YES;
}

But the activity indicator does not show up at all? In the method "doThisForTap" i do computation and move to another UIViewController. But i cannot see the activity indicator. What am i doing wrong? If you need more info,please ask. Thanks..

RookieAppler
  • 1,517
  • 5
  • 22
  • 58
  • 1
    I thought that NSThread couldn't be used in UIKit (no-thread safe). – Larme Jan 17 '13 at 21:23
  • @Larme. This is the thread i mentioned in my question http://stackoverflow.com/questions/1850186/iphone-uiactivityindicatorview-not-starting-or-stopping – RookieAppler Jan 17 '13 at 21:44
  • Well, I'd do the animation in the mainthread, and the loading or whatever you're want to do meanwhile in another thread if it doesn't touch the UI. You could "resynchronize" sometimes to do a small animation between the final end and a step. – Larme Jan 17 '13 at 21:47
  • @Larme correct. he should not be doing what he's doing. – jsd Jan 17 '13 at 23:25

1 Answers1

1

It doesn't appear as though you're actually adding the indicator to the view hierarchy with addSubview:

You're instantiating it, assigning it to a property, and starting it animating, but never actually adding it to a view hierarchy (as far as I can see).

To add an activity indicator into the screen, you should set its origin and then add it to whichever view it should appear in:

self.activityIndicatorNew =[[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
self.activityIndicatorNew.color = [UIColor redColor];

CGRect indicatorFrame = self.activityIndicatorNew.frame;
indicatorFrame.origin.x = // x coordinate goes here;
indicatorFrame.origin.y = // y coordinate goes here;
self.activityIndicatorNew.frame = indicatorFrame;

[self.view addSubview:self.activityIndicatorNew];

[self.activityIndicatorNew startAnimating];

You shouldn't be doing most of the above in a background thread, as you're not supposed to manipulate the view hierarchy from anything but the main thread.

If you really do need to start the indicator animating in a background thread (not entirely convinced you do from the code that you've shown), then the only thing it is safe to do in that background thread is to call startAnimating. Everything else should go into viewDidLoad before you detach the new thread.

However, I'd try doing it all in viewDidLoad first and only use the background thread if absolutely necessary.

Personally, I'd use Interface Builder for this; I can't think of many reasons to instantiate a simple activity indicator in code.

petemorris
  • 516
  • 4
  • 4
  • Thanks.. i just abandoned all that i was doing. Just dropped a UIActivityIndicator on my storyboard and went from there. It works..i did this yesterday..it wouldnt work..Thanks again. – RookieAppler Jan 17 '13 at 23:29