我已经阅读了很多有关此的内容,但似乎找不到与我的情况相符的内容。

当 View 出现时,我已经加载了MBProgressHUD,因为我的应用程序立即获取了一些Web服务数据。我的问题是,在显示HUD时(因此在应用获取数据时),导航 Controller 上的后退按钮无响应。我希望用户可以轻按一下以消除干扰(或者在最坏的情况下也可以单击“后退”按钮),以防万一,这是无尽的等待。这是在 View 出现后立即运行的代码:

#ifdef __BLOCKS__
MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.navigationController.view animated:YES];
hud.labelText = @"Loading";
hud.dimBackground = NO;
hud.userInteractionEnabled = YES;

dispatch_async(dispatch_get_global_queue( DISPATCH_QUEUE_PRIORITY_LOW, 0), ^{
    // Do a task in the background
    NSString *strURL = @"http://WEBSERVICE_URL_HERE";

    //All the usual stuff to get the data from the service in here

    NSDictionary* responseDict = [json objectForKey:@"data"]; // Get the dictionary
    NSArray* resultsArray = [responseDict objectForKey:@"key"];


    // Hide the HUD in the main tread
    dispatch_async(dispatch_get_main_queue(), ^{

        for (NSDictionary* internalDict in resultsArray)
        {
            for (NSString *key in [internalDict allKeys])
            {//Parse everything and display the results
            }

        }

        [MBProgressHUD hideHUDForView:self.navigationController.view animated:YES];
    });
});
#endif

省略了所有有关解析JSON的胡言乱语。所有这些都工作正常,并且HUD在显示数据并显示后关闭。我到底该如何启用一种方法来停止所有这些操作,然后返回(空白)界面?手势识别器?我可以在MBProgressHUD类中进行设置吗?好沮丧...

衷心感谢您的帮助。对于长期的帖子,我深表歉意。而对于我的丑陋代码...

最佳答案

无需扩展MBProgressHUD。只需添加一个UITapGestureRecognizer即可。

ViewDidLoad
:

MBProgressHUD *HUD = [MBProgressHUD showHUDAddedTo:self.view animated:NO];
HUD.mode = MBProgressHUDModeAnnularDeterminate;

UITapGestureRecognizer *HUDSingleTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(singleTap:)];
[HUD addGestureRecognizer:HUDSingleTap];

然后:
-(void)singleTap:(UITapGestureRecognizer*)sender
{
      //do what you need.
}

关于ios - JSON调用时间过长时,允许用户取消MBProgressHUD,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/13212467/

10-13 03:50