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

gum*_*mmo 6 xcode json uigesturerecognizer ios mbprogresshud

我已经阅读并阅读了关于此的内容,我似乎无法找到符合我情况的任何内容.

当视图出现时我已经加载了MBProgressHUD,因为我的应用程序会立即获取一些web服务数据.我的问题是我的导航控制器上的后退按钮在显示HUD时没有响应(因此当应用程序获取其数据时).我希望用户能够点击以解除(或者在最坏的情况下能够点击后退按钮)以获得结果,如果这是无休止的等待.这是我的代码,只要视图出现就会运行:

#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
Run Code Online (Sandbox Code Playgroud)

抛弃解析JSON的所有胡言乱语.这一切都运行正常,HUD在数据显示并显示后解散.在世界上我如何能够在点击时停止所有这些并返回(空白)界面?GestureRecognizer?我会在MBProgressHUD类中设置它吗?太沮丧......

衷心感谢任何帮助.我为长篇大论道歉.而对于我丑陋的代码......

Pio*_*sik 35

无需延长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];
Run Code Online (Sandbox Code Playgroud)

然后:

-(void)singleTap:(UITapGestureRecognizer*)sender
{
      //do what you need.
}
Run Code Online (Sandbox Code Playgroud)


小智 5

MBProgressHUD只是一个带有自定义绘图的视图,用于指示当前进度,这意味着它不对您的任何应用程序逻辑负责.如果你有一个需要在某个时候取消的长时间运行操作,你必须自己实现.

最优雅的解决方案是扩展MBProgressHUD.您可以绘制一个扮演按钮角色的自定义区域,以编程方式添加按钮或只是等待整个视图上的点击事件.然后,只要轻触该按钮或视图,就可以调用委托方法.

它看起来像这样:

 // MBProgressHUD.h
 @protocol MBProgressHUDDelegate <NSObject>
 - (void)hudViewWasTapped; // or any other name 
 @end

// MBProgressHUD.m
// Either this, or some selector you set up for a gesture recognizer
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
    if ([self.delegate respondsToSelector:@selector(hudViewWasTapped)]) {
        [self.delegate performSelector:@selector(hudViewWasTapped)];
    }
}
Run Code Online (Sandbox Code Playgroud)

您必须将视图控制器设置为其代理MBProgressHUD并相应地执行操作.

如果您需要更多澄清,请告诉我:)