Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How show activity-indicator when press button for upload next view or webview?

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?

like image 576
ram Avatar asked Dec 28 '22 16:12

ram


2 Answers

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

like image 177
Karoly S Avatar answered Dec 31 '22 12:12

Karoly S


[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.

like image 42
AechoLiu Avatar answered Dec 31 '22 13:12

AechoLiu