Ios 允许用户在JSON调用时间过长时取消MBProgressHUD
我读了很多关于这方面的书,但我似乎找不到任何符合我的情况的东西 当视图出现时,我会加载MBProgressHUD,因为我的应用程序会立即获取一些Web服务数据。我的问题是,当显示HUD时(因此当应用程序获取数据时),navigationcontroller上的后退按钮没有响应。我希望用户能够点击关闭按钮(或者在最坏的情况下点击后退按钮)退出,如果这是一个无休止的等待。以下是我的代码,该代码在视图出现时立即运行:Ios 允许用户在JSON调用时间过长时取消MBProgressHUD,ios,xcode,json,uigesturerecognizer,mbprogresshud,Ios,Xcode,Json,Uigesturerecognizer,Mbprogresshud,我读了很多关于这方面的书,但我似乎找不到任何符合我的情况的东西 当视图出现时,我会加载MBProgressHUD,因为我的应用程序会立即获取一些Web服务数据。我的问题是,当显示HUD时(因此当应用程序获取数据时),navigationcontroller上的后退按钮没有响应。我希望用户能够点击关闭按钮(或者在最坏的情况下点击后退按钮)退出,如果这是一个无休止的等待。以下是我的代码,该代码在视图出现时立即运行: #ifdef __BLOCKS__ MBProgressHUD *hud = [MB
#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只是一个带有自定义图形的视图,用于指示当前进度,这意味着它不负责应用程序的任何逻辑。如果您有一个长时间运行的操作需要在某个时候取消,那么您必须自己实现它 最优雅的解决方案是扩展
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)];
}
}
//MBProgressHUD.h
@协议代理
-(无效)HUDVIEW被点击;//或任何其他名称
@结束
//MBProgressHUD.m
//或者是这个,或者是为手势识别器设置的一些选择器
-(void)touchesend:(NSSet*)toucheevent:(UIEvent*)event{
if([self.delegate respondsToSelector:@selector(hudviewwasttapped)]){
[self.delegate performSelector:@selector(hudviewwasttapped)];
}
}
您必须将视图控制器设置为MBProgressHUD
的代理,并采取相应的行动
如果您需要更多说明,请告诉我:)无需扩展
MBProgressHUD
。只需向其添加一个uitappesturerecognizer
ViewDidLoad
:
然后:
-(void)singleTap:(UITapGestureRecognizer*)sender
{
//do what you need.
}
要获得更多信息:
-(void)singleTap:(UITapGestureRecognizer*)sender
{
//do what you need.
}
这样,navigationBar的视图将不对HUD视图负责。因此,您可以从navigationController的视图返回到上一页。一个问题是,如果父视图触动,它也会调用