Uiactivityindicatorview does not work

Asked

Viewed 120 times

3

I want to display the activity after the click of a button. This button will update the app. I followed examples and documentation, but it doesn’t work...

- (void)viewDidLoad {
    // .. snip
    indicator = [[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
    [self.view addSubview:indicator];
    [indicator stopAnimating];
    //indicator.hidden = TRUE;
    //[indicator setHidden:YES];
}

-(IBAction)actAtualizar:(id)sender {
    //[indicator setHidden:YES];
    //indicator.hidden = FALSE;
    [indicator startAnimating];
    //[indicator setHidden:NO];
    //[indicator stopAnimating];
    //indicator.hidden = TRUE;
    [self carregaDados];
    NSLog(@"teste de atualizacao", nil);
}


-(void)carregaDados{
    // ...     
    [indicator stopAnimating];
}

2 answers

1

I believe that when you do startAnimation, the interface can only be updated in fact when you finish an iteration of runloop ios. The problem is that there is no time for the animation to start as it is posted the startAnimation event, then the loaded methodDados runs until the end and then is posted the event to finish the animation. When events are actually processed you have already executed your method.

There are some ways to solve this, the most recommended would run a method that takes too long on a separate thread, not to lock the interface. That way you can start the animation, shoot the loaded thread and when you are done giving the stopAnimation. Thus, the bar will be animated while processing the loaded.

Another way, is to make the loaded Data run for sure after start Animation. An example of how to do this is:

[indicator startAnimating];
[self performSelector:@selector(carregaDados) withObject:nil afterDelay:0.01];

...

- (void)carregaDados{
    ...
    [indicator stopAnimating];
}

Remember that it is not recommended to run a method that stops the application and the user has to wait. Imagine a tableView that he can’t even scroll because the UI is stuck.

References:

https://stackoverflow.com/questions/1850186/iphone-uiactivityindicatorview-not-starting-or-stopping

https://stackoverflow.com/questions/2391343/run-an-uiactivityindicatorview-while-the-main-thread-is-busy

https://stackoverflow.com/questions/3317068/how-do-i-show-and-start-animating-a-uiactivityindicatorview-from-within-a-method

https://developer.apple.com/library/ios/documentation/cocoa/reference/foundation/Classes/NSObject_Class/Reference/Reference.html#//apple_ref/occ/instm/NSObject/performSelector:withObject:afterDelay:

  • 1

    I got... my object was not related to the view and not to the Indicator,,... very cool your answer Valew

0

From what I understand you created the ActivityIndicatorView via code, i.e., did not add the component to the .xib.

In this case you need to define the frame of your object on the screen:

//Por padrão se você NÃO definir o frame o objeto será colocado na posição: (0,0)
indicator = [[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
[indicator setFrame:CGRectMake(100, 100, indicator.frame.size.width, indicator.frame.size.height)]; 
. . .

If you have the added component in .xib just comment on the following lines of your code:

//Comentar as linhas abaixo
>>> indicator = [[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
>>> [self.view addSubview:indicator];
. . .

The fact that you put the ActivityIndicatorView right in the .xib already defined the object allocation.

  • no . h this so @Property (Weak, nonatomic) Iboutlet Uiactivityindicatorview *Indicator;

  • iTSangar excuses ignorance because I’m starting in developmentIos what is . xib, is equivalent to what in android

  • 1

    got... my object was not related to the view and not to the Indicator,,,... xib is used in ancient mode got Jah... Valew

Browser other questions tagged

You are not signed in. Login or sign up in order to post.