Activity indicator (spinner) with UIActivityIndicatorView

前端 未结 4 539
耶瑟儿~
耶瑟儿~ 2021-02-04 21:32

I have a tableView that loads an XML feed as follows:

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];

    if ([stories count] == 0) {         


        
4条回答
  •  渐次进展
    2021-02-04 22:13

    I typically implement an NSTimer that will call my spinner method, which I fire off right before I go into doing the heavy work (the work that will typically block the main thread).

    The NSTimer fires and my spinner method is called. When the main work is finished, I disable the spinner.

    Code for that is like:

    IBOutlet UIActiviyIndicatorView *loginIndicator;
    
    {
        ...
        [loginIndicator startAnimating];
    
        [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(executeAuthenticationRequest) 
                                       userInfo:nil repeats:NO];   
        ...
    }
    
    - (void) executeAuthenticationRequest
    {
        /* Simulate doing a network call. */
        sleep(3);
    
        [loginIndicator stopAnimating];
    
        ...
    }
    

    You can also do:

    IBOutlet NSProgressIndicator *pIndicator;
    

    Start:

    [pIndicator startAnimation:self];
    [pIndicator setHidden:NO];
    

    And Stop:

    [pIndicator stopAnimation:self];
    [pIndicator setHidden:YES];
    

提交回复
热议问题