HUD在执行segue时显示

时间:2012-04-24 21:33:42

标签: iphone objective-c ios xcode ipad

我在我的应用程序上使用了master-detail示例。

我添加了MBProgressHUD以显示加载屏幕,同时加载详细信息。

问题是我不知道我对线程做错了什么,但我最终有两种方法:

1 - 如果我不抛出dispatch_async(),则显示HUD延迟;

2 - 如果我在dispatch_async()中执行segue,则需要更多时间来加载东西。

Heres da code for example 1:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [HUD show:YES];
    [self performSegueWithIdentifier:@"detail" sender:nil];
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

Heres da code for example 2:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [HUD show:YES];
    dispatch_queue_t taskQ = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0);
    dispatch_async(taskQ, ^{
        [self performSegueWithIdentifier:@"detail" sender:nil];
        [tableView deselectRowAtIndexPath:indexPath animated:YES];
    });
}

任何线索?

2 个答案:

答案 0 :(得分:0)

这是一个对我有用的解决方案:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    HUD = [MBProgressHUD showHUDAddedTo:((AppDelegate*)[[UIApplication sharedApplication] delegate]).window.rootViewController.view animated:YES];
    HUD.labelText = @"Loading";
    HUD.labelFont = [UIFont systemFontOfSize:18];
    HUD.delegate = self;
    [HUD showWhileExecuting:@selector(openYourNewView) onTarget:self withObject:nil animated:YES];
    });
}

-(void)openYourNewView {
    [self performSegueWithIdentifier:@"YourViewIdentifier" sender:self];
}

但是,我在之前的视图中仍然有一些奇怪的轮换,我不知道为什么。

答案 1 :(得分:0)

我以某种方式解决了这个问题!

  1. 创建一个常规方法,您将调用progressHUD并调用第二个

  2. 创建第二种方法,用于执行耗时的工作(加载视图)

  3. 在主线程上执行该方法

  4. 样品:

    -(void)callHUD {
    
                [progressHUD show];
    
                dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
                    [self performSelectorOnMainThread:@selector(loadView) withObject:nil waitUntilDone:YES];
                    dispatch_async(dispatch_get_main_queue(), ^{
                [progressHUD dismiss];
        });
    });
    }
    
    -(void)loadView {
        //Perform your segue or transition which needs to load
    }
    

    希望能帮助其他人寻找答案。 ;)干杯