4

My first view is looks like  this when i click on button which title is click here to enlarge then i want show activity indicator on the first view and remove when load this view. second view in which image is upload from URL in image view.

but i go back then it show activity indicator which is shown in this view. first view with activity indicator

in first vie .m file i have use this code for action.

-(IBAction)btnSelected:(id)sender{
UIButton *button = (UIButton *)sender;
int whichButton = button.tag;
NSLog(@"Current TAG: %i", whichButton);
UIActivityIndicatorView *spinner = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
[spinner setCenter:CGPointMake(160,124)]; 
[self.view addSubview:spinner]; 
[spinner startAnimating];

if(whichButton==1)
{
    [spinner stopAnimating];

    first=[[FirstImage alloc]init];
    [self.navigationController pushViewController:first animated:YES];
    [spinner hidesWhenStopped ];

        }} 

in above code i have button action in which i call next view. Now i want show/display activity indicator when view upload. In next view i have a image view in which a image i upload i have declare an activity indicator which also not working. How do that?

Karoly S
  • 3,180
  • 4
  • 35
  • 55
ram
  • 1,193
  • 1
  • 15
  • 27

3 Answers3

9

Toro's suggestion offers a great explanation and solution, but I just wanted to offer up another way of achieving this, as this is how I do it.

As Toro said,

- (void) someFunction
{
    [activityIndicator startAnimation];

    // do computations ....

    [activityIndicator stopAnimation];  
}

The above code will not work because you do not give the UI time to update when you include the activityIndicator in your currently running function. So what I and many others do is break it up into a separate thread like so:

- (void) yourMainFunction {
    activityIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];

    [NSThread detachNewThreadSelector:@selector(threadStartAnimating) toTarget:self withObject:nil];

    //Your computations

    [activityIndicator stopAnimating];

}

- (void) threadStartAnimating {
    [activityIndicator startAnimating];
}

Good luck! -Karoly

Karoly S
  • 3,180
  • 4
  • 35
  • 55
  • it is not working i have edit my question so u check it again . – ram Jul 27 '11 at 05:28
  • 1
    I don't like break into separate methods or threads, so I often add [[NSRunLoop currentRunLoop] runUntilDate:[NSDate date]] after start animating. – AechoLiu Jul 27 '11 at 12:21
1
[self.navigationController pushViewController:first animated:YES];

Generally, when you push a view controller into navigation controller, it will invoke the -(void)viewWillAppear: and -(void)viewDidAppear: methods. You can add activity indicator view inside the viewWillAppear: and call startAnimation of indicator view. You CANNOT invoke startAnimation and stopAnimation at the same time. For example,

- (void)viewWillAppear:(BOOL)animated 
{
    [aIndicatorView startAnimation];

    // do somethings ....

    [aIndicatorView stopAnimation];  
}

Because the startAnimation and stopAnimation are under the same time, then no animation will show.

But if you invoke startAnimation in -(void)viewWillAppear: and invoke stopAnimation in another message, like followings.

- (void)viewWillAppear:(BOOL)animated
{
    [aIndicatorView startAnimation];

    // do somethings... 
}

- (void)viewDidAppear:(BOOL)animated
{
    [aIndicatorView stopAnimation];
}

Because viewWillAppear: and viewDidAppear: are invoked with different event time, the activity indicator view will work well.

Or, you can do something like followings:

- (void)viewWillAppear:(BOOL)animated 
{
    [aIndicatorView startAnimation];

    // Let run loop has chances to animations, others events in run loop queue, and ... etc. 
    [[NSRunLoop currentRunLoop] runUntilDate:[NSDate date]];

    // do somethings ....

    [aIndicatorView stopAnimation];
}

The above example is a bad example, because it invokes two or more animations in the -runUntilDate:. But it will let the activity indicator view work.

AechoLiu
  • 17,522
  • 9
  • 100
  • 118
0

Create a webview. Add a activity indicator to the webview. If you are loading a image via url into the webview then implement the webview delegate methods. Once the url is loaded then stopanimating the activity indicator.

Let me know which step you are not able to implement.

Praveen S
  • 10,355
  • 2
  • 43
  • 69