调用hide后,MBProgressHUD不会消失

Chr*_*ton 4 objective-c ios

我确信这是我的iOS/ObjC noob-ness的一个问题...

我有一个带有条目的UITableView,当用户选择一行时,

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
...
   [self.twitter sendUpdate:[self getTweet]];
Run Code Online (Sandbox Code Playgroud)

- (NSString *)sendUpdate:(NSString *)text;
{
    NSLog(@"showing HUD");
    self.progressSheet = [MBProgressHUD showHUDAddedTo:[[UIApplication sharedApplication] keyWindow] animated:YES];
    self.progressSheet.labelText = @"Working:";
    self.progressSheet.detailsLabelText = text;

    // Build a twitter request
    TWRequest *postRequest = [[TWRequest alloc] initWithURL:
                              [NSURL URLWithString:@"http://api.twitter.com/1/statuses/update.json"] 
                                                 parameters:[NSDictionary dictionaryWithObject:text 
                                                                                        forKey:@"status"] requestMethod:TWRequestMethodPOST];

    // Post the request
    [postRequest setAccount:self.twitterAccount];

    // Block handler to manage the response
    [postRequest performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error) 
     {
         NSLog(@"Twitter response, HTTP response: %i", [urlResponse statusCode]);

           NSLog(@"hiding HUD");
           [MBProgressHUD hideHUDForView:[[UIApplication sharedApplication] keyWindow] animated:YES];
           self.progressSheet = nil;
Run Code Online (Sandbox Code Playgroud)

它调用内置的Twitter api发送推文.发送过程中我正在使用MBProgressHUD.随着HUD消失,我的行为变得不稳定,通常它比它应该延迟大约10秒左右.根据我看到的显示/隐藏日志记录.

我有另一个更简单的视图,它只列出推文并且使用HUD没有问题 - 尽管它是通过viewWillAppear调用完成的.

也许我需要通过另一个线程进行显示?

提前感谢任何想法〜克里斯

Sta*_*tan 11

是的,你是对的ui线程.你也可以这样写:

dispatch_async(dispatch_get_main_queue(), ^{
  [self.progressSheet hide:YES];
  self.progressSheet = nil;
});
Run Code Online (Sandbox Code Playgroud)


Chr*_*ton 5

似乎我的问题是我试图在主要线程以外的线程上关闭HUD.

在这个问题的一个答案中使用这个技巧,它现在工作得更好.

GCD在主线程中执行任务

即,使用定义的方法"runOnMainQueueWithoutDeadlocking"

关闭对话框代码现在是这样的:

runOnMainQueueWithoutDeadlocking(^{
    NSLog(@"hiding HUD/mainthread");
    [self.progressSheet hide:YES];
    self.progressSheet = nil;
});
Run Code Online (Sandbox Code Playgroud)