Search code examples
iphoneobjective-cnstimer

How to set Activity indicator with timer


How can i set activity indicator with nstimer in my apps.

Thanks in Advance:


Solution

  • In header file you need to declare activityIndicator and activityTimer.

    - (void)viewDidLoad {
        [super viewDidLoad];
    
        activityIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
    
        activityIndicator.frame = CGRectMake(150,200, 20, 20);
    
        [self.view addSubview:activityIndicator];
    
        [activityIndicator startAnimating];
    
        [activityTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(stopActivity:) userInfo:nil repeats:NO];
        [activityIndicator release];
    }
    -(void) stopActivity:(NSTimer *) theTimer
    {
      [activityIndicator stopAnimating];
      [activityTimer invalidate];
      activityTimer = nil;
    }