ios - 如何在执行 segue 期间使用 UIActivityIndi​​catorView?

标签 ios objective-c uiactivityindicatorview didselectrowatindexpath

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

我无法实现 UIActivityIndi​​cator,当我点击 AMSlideMenu 中的表格单元格时,加载另一个 View 需要时间,因为数据正在加载到另一个 View 中。

我想在用户单击单元格 UIActivityIndi​​cator 时提供 UIActivityIndi​​cator 执行,直到它打开另一个 View 。

这是我的代码:

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

最佳答案

在类级别范围内声明 UIActivityIndi​​catorView

UIActivityIndicatorView *spinner;

在主队列中执行segue:

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

在进行 segue 时停止微调器:

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

关于ios - 如何在执行 segue 期间使用 UIActivityIndi​​catorView?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25666115/

相关文章:

iphone - 从 XSD 方案生成 objective-c 类,如 Java 中的 JaxB?

objective-c - UIButton里面的UIActivityIndi​​catorView

ios - dateFromString() 返回错误的日期 swift 3.0

ios - 当我点击 uitableviewcell 中的 uiimageview 时,应用程序崩溃

ios - 夸大 CATransform3D Perspective 的透视图

ios - UIActivityIndi​​catorView 仅在 Release模式下崩溃

ios - 如何向 UITextView 添加事件指示器?

ios - 添加新的初始 UIViewController 之后 UINavigationController "acting crazy"

objective-c - 子类 NSView 未出现在自定义 View 中

iphone - 释放 NSURLConnection 对象