How to UIActivityIndicatorView during perform segue?

拜拜、爱过 提交于 2019-12-12 02:58:29

问题


I’m building an article reading app. I’m using AMSliderMenu(https://github.com/SocialObjects-Software/AMSlideMenu) library for menu list.

I am unable to implement UIActivityIndicator, when i click on table cell in AMSlideMenu it takes time to load another view because data is load into anther view.

I want to give UIActivityIndicator when user click on cell UIActivityIndicator perform till the time it opens another view.

Here is my code:

      - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath  *)indexPath
      {

            UIActivityIndicatorView *spinner = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
          spinner.center = CGPointMake(160, 240);
          spinner.hidesWhenStopped = YES;
          [self.view addSubview:spinner];
         [spinner startAnimating];
          dispatch_queue_t downloadQueue = dispatch_queue_create("downloader", NULL);
          dispatch_async(downloadQueue, ^{
         [NSThread sleepForTimeInterval:10];
        dispatch_async(dispatch_get_main_queue(), ^{
       [spinner stopAnimating];

         });

    });
     NSString *segueIdentifier = [self.mainVC segueIdentifierForIndexPathInLeftMenu:indexPath];

       if (segueIdentifier && segueIdentifier.length > 0)
      {
        [self performSegueWithIdentifier:segueIdentifier sender:self];
          }
        }

回答1:


Declare the UIActivityIndicatorView in the class level scope.

UIActivityIndicatorView *spinner;

Perform the segue in the main queue:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath  *)indexPath
{
    spinner = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
    spinner.center = CGPointMake(160, 240);
    spinner.hidesWhenStopped = YES;
    [self.view addSubview:spinner];
    [spinner startAnimating];
    dispatch_queue_t downloadQueue = dispatch_queue_create("downloader", NULL);
    dispatch_async(downloadQueue, ^{
    [NSThread sleepForTimeInterval:10];
        dispatch_async(dispatch_get_main_queue(), ^{
            NSString *segueIdentifier = [self.mainVC segueIdentifierForIndexPathInLeftMenu:indexPath];
            if (segueIdentifier && segueIdentifier.length > 0){
                [self performSegueWithIdentifier:segueIdentifier sender:self];
            }else{
                [spinner stopAnimating];
            }
        });
    });
}

Stop the spinner when doing the segue:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    [spinner stopAnimating];
}


来源:https://stackoverflow.com/questions/25666115/how-to-uiactivityindicatorview-during-perform-segue

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!