UIActivityIndicatorView does not work

3

I want to display the activity indicator after clicking a button. This button will update the app. I followed examples and documentation, but it does not 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];
}
    
asked by anonymous 14.02.2014 / 12:54

2 answers

1

I believe that when you start startAnimation, the interface can only actually be updated when an IOS runloop iteration ends. The problem is that there is no time for the animation to start, because the startAnimation event is posted, then the method loadsDate runs to the end and then the event is posted to end the animation. When events are in fact being processed you have already performed your method.

There are some ways to resolve this, the most recommended would be to run a method that takes a long time on a separate thread, so as not to crash the interface. That way, you can start the animation, fire the uploads in another thread and when you end up giving the stopAnimation. So, the bar will get animated while you are processing the loads.

Another way , is to make the uploadData be executed 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];
}

Remembering that it is not recommended to execute a method that locks the application and the user has to wait. Imagine a tableView that it can not even scroll because the UI is locked.

References :

link

link

a>

link :

    
14.02.2014 / 14:05
0

I noticed that you created ActivityIndicatorView via code, that is, you did not add the component to .xib .

In this case you need to set 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 component added in .xib just comment the following lines of your code:

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

The fact that you have set ActivityIndicatorView direct to .xib is already set to object allocation.

    
14.02.2014 / 13:54