我使用以下代码来点击图片视图并导航到另一个视图控制器:
-(void)tapDetected:(UIGestureRecognizer *)recognizer{
[MBProgressHUD showHUDAddedTo:self.view animated:YES];
NSLog(@"single Tap on imageview");
ImageTapGesture *sender = (ImageTapGesture *)recognizer;
UIStoryboard * storyboard = self.storyboard;
tele * detail = [storyboard instantiateViewControllerWithIdentifier: @ "teledramaView"];
detail.imagelink = sender.imageLink;
detail.description = sender.description;
detail.id = sender.id;
[self.navigationController pushViewController: detail animated: YES];
[MBProgressHUD hideHUDForView:self.view animated:YES];
}
到目前为止,代码工作正常,但问题是进度条无法正常显示。
它会在转到其他控制器之前的最后时刻显示,但日志出现在进度条之前:NSLog(@"single Tap on imageview");
有人可以告诉我为什么进度条不能正常工作吗?谢谢。
答案 0 :(得分:1)
答案很简单,因为您在同一个线程中显示并隐藏了progressHUD,它会在添加后立即删除。由于您在主线程上执行了方法,这也是处理任何UI更改的方法,因此只有在方法结束后才会添加progressHUD。但是,由于你删除了最后一行的progressHUD,它也将被直接删除。更糟糕的是,添加和删除HUD之间的任何代码都会阻止任何UI更改。
您需要稍微延迟加载,以便系统有时间显示progressHUD:
-(void)tapDetected:(UIGestureRecognizer *)recognizer{
[MBProgressHUD showHUDAddedTo:self.view animated:YES];
NSLog(@"single Tap on imageview");
[self performSelector:@selector(navigateToDetailView:) withObject:recognizer afterDelay:0.0];
}
-(void) navigateToDetailView:(UIGestureRecognizer*)recognizer{
ImageTapGesture *sender = (ImageTapGesture *)recognizer;
UIStoryboard * storyboard = self.storyboard;
tele * detail = [storyboard instantiateViewControllerWithIdentifier: @ "teledramaView"];
detail.imagelink = sender.imageLink;
detail.description = sender.description;
detail.id = sender.id;
[self.navigationController pushViewController: detail animated: YES];
[MBProgressHUD hideHUDForView:self.view animated:YES];
}