我正在使用 SVProgressHUD 类 ( https://github.com/samvermette/SVProgressHUD ),我得到了一个带有按钮的主视图 Controller ,该按钮使用 segue 推送与另一个 View Controller 连接。 在主视图 Controller 中,我添加了这段代码:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
[SVProgressHUD showWithStatus:@"Loading"];
NSLog(@"segue test");
}
我想要做的是,在加载其他 View Controller 之前,需要显示 HUD。如果我运行我的程序,它首先打印出 NSLog“segue test”,然后从另一个 View Controller 打印出 NSLogs,问题是当按下按钮时 HUD 不直接显示,它显示另一个 View Controller 已加载...
这是我现在拥有的:
这是我需要的:
蓝屏加载后,“正在加载”HUD 需要消失。
最佳答案
您可以从 View Controller 类连接 segue,而不是直接从按钮连接 segue。确保为 segue 命名,因为您需要此名称以便稍后调用它。
然后,您可以首先将您的按钮连接到一个 IBAction
,它将首先加载您正在加载的内容。加载完成后,您可以关闭进度 HUD 并调用 segue。
- (IBAction)loadStuff:(id)sender
{
[SVProgressHUD showWithStatus:@"Loading"];
[self retrieveStuff];
}
- (void)retrieveStuff
{
// I'll assume you are making a NSURLConnection to a web service here, and you are using the "old" methods instead of +[NSURLConnection sendAsynchronousRequest...]
NSURLConnection *connection = [NSURLConnection connectionWith...];
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
// Do stuff with what you have retrieved here
[SVProgressHUD dismiss];
[self performSegueWithIdentifier:@"PushSegueToBlueScreen"
sender:nil];
}
如果你只是想先模拟会发生什么,你可以试试这个:
- (IBAction)loadStuff:(id)sender
{
[SVProgressHUD showWithStatus:@"Loading"];
[self retrieveStuff];
}
- (void)retrieveStuff
{
[NSTimer scheduledTimerWithTimeInterval:2 // seconds
target:self
selector:@selector(hideProgressHUDAndPush)
userInfo:nil
repeats:NO];
}
- (void)hideProgressHUDAndPush
{
// Do stuff with what you have retrieved here
[SVProgressHUD dismiss];
[self performSegueWithIdentifier:@"PushSegueToBlueScreen"
sender:nil];
}
编辑:您可以尝试使用此 GCD block 来下载单个图像。我认为您可以修改一下,以便支持下载多个图像。
- (IBAction)loadStuff:(id)sender
{
[SVProgressHUD showWithStatus:@"Loading"];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0),
^{
// ... download image here
[UIImagePNGRepresentation(image) writeToFile:path
atomically:YES];
dispatch_sync(dispatch_get_main_queue(),
^{
[SVProgressHUD dismiss];
[self performSegueWithIdentifier:@"PushSegueToBlueScreen"
sender:nil];
});
});
}
关于ios - 进度 HUD 未显示在正确的 View Controller 上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15566408/