进度HUD未在正确的视图控制器上显示

时间:2013-03-22 09:05:51

标签: ios objective-c xcode

我正在使用SVProgressHUD类(https://github.com/samvermette/SVProgressHUD),我有一个主视图控制器,上面有一个按钮,它使用segue push与另一个视图控制器连接。 在主视图控制器中,我添加了以下代码:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
   [SVProgressHUD showWithStatus:@"Loading"];
   NSLog(@"segue test");
}

我想要做的是,在加载其他View Controller之前,需要显示HUD。如果我运行我的程序,它首先打印出NSLog“segue test”,之后它从另一个View Controller打印出NSLogs,问题是当按下按钮时HUD没有直接打开,它显示的是另一个视图控制器已加载...

这就是我现在所拥有的:

http://i47.tinypic.com/jphh0n.png

http://i50.tinypic.com/vzx16a.png

这就是我需要的:

http://i45.tinypic.com/2vcb1aw.png

蓝屏加载后,“加载”HUD需要消失。

1 个答案:

答案 0 :(得分:3)

您可以从视图控制器类连接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块下载单张图像。我想你可以修补一下,这样你就可以支持下载多张图片了。

- (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];
                                       });
                    });
}