ios - 当 JSON 调用时间过长时允许用户取消 MBProgressHUD

标签 ios xcode json uigesturerecognizer mbprogresshud

我已经阅读和阅读了关于此的内容,但我似乎找不到任何符合我情况的内容。

当 View 出现时,我已经加载了 MBProgressHUD,因为我的应用程序会立即获取一些网络服务数据。我的问题是我的导航 Controller 上的后退按钮在显示 HUD 时没有响应(因此在应用程序获取其数据时)。我希望用户能够点击关闭(或者在最坏的情况下能够点击后退按钮)以摆脱困境,如果这是无休止的等待。这是我在 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/

相关文章:

ios - 将投影矩阵移动到特定的 X、Y、Z 点? OpenGLES 2.0 - iOS

ios - UISearchController 使用 Swift 2 在另一个数组中搜索数组项

ios - 使用模型无法在 cellForRowAtIndexPath 中获取数据

ruby-on-rails - rails Jbuilder : how to format an array of one item as a JSON array?

json - 使用连接参数的Elasticsearch存储桶聚合

ios - 加载 Viewcontroller 的新部分

iphone - 将 Urbanairship 集成到我的 iOS 应用程序中所需的最少文件集是什么?

ios - XCUITest 在页面上找不到元素

ios - Xcode 更改未修改的 Storyboard和 XIB 文件

Xcode 窗口组织技巧?