【问题标题】:How to UIActivityIndicatorView during perform segue?如何在执行 segue 期间 UIActivityIndi​​catorView?
【发布时间】:2025-12-26 07:25:11
【问题描述】:

我正在构建一个文章阅读应用程序。我正在使用 AMSliderMenu(https://github.com/SocialObjects-Software/AMSlideMenu) 菜单列表库。

我无法实现UIActivityIndicator,当我点击AMSlideMenu中的表格单元格时,加载另一个视图需要时间,因为数据已加载到另一个视图中。

当用户单击单元格UIActivityIndicator 时,我想给UIActivityIndicator 执行直到它打开另一个视图。

这是我的代码:

      - (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];
          }
        }

【问题讨论】:

  • 按下一个单元格会发生什么?顺便说一句,考虑 MBProgressHud github.com/jdg/MBProgressHUD
  • 你为什么不使用 MBProgressHUD?它是最好的之一

标签: ios objective-c uiactivityindicatorview didselectrowatindexpath


【解决方案1】:

在类级别范围内声明UIActivityIndicatorView

UIActivityIndicatorView *spinner;

在主队列中执行转场:

- (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];
            }
        });
    });
}

在进行转场时停止微调器:

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

【讨论】:

    最近更新 更多