【问题标题】:Progress HUD doesn't show on the correct View Controller进度 HUD 未显示在正确的视图控制器上
【发布时间】:2013-03-22 09:05:50
【问题描述】:

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

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

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

这就是我现在拥有的:

这是我需要的:

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

【问题讨论】:

    标签: ios objective-c xcode


    【解决方案1】:

    您可以从视图控制器类连接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];
                                           });
                        });
    }
    

    【讨论】:

    • 我没有使用 NSURLConnection,所以第二个选项对我来说会更好。我不需要等待连接,我需要等待第二个视图控制器(蓝色)。在那个类中,我从互联网上下载了一些图像,所以如果我按下主视图控制器上的按钮,它需要显示正在加载的 HUD,并且在加载蓝色视图控制器图像后,HUD 需要消失并且蓝色需要弹出。
    • 问题是你给了“秒”,但不知道下载所有图片需要多长时间...
    • 您是否正在使用一些“框架”(例如 ASIHTTPRequest)来下载您的图像?如果是这样,应该有一些方法或块可以放置连接完成后应该执行的操作。我的意思是,由于您正在下载图像,因此您应该关闭进度HUD并在下载后进入蓝屏;您不应使用选项 2,因为完成下载的时间可能会有所不同。
    • 我没有在这个上使用 ASIHTTPRequest,我使用的是普通的 NSFileManager 和 [pngData writeToFile:filePath atomically:YES];
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-06-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-25
    • 1970-01-01
    相关资源
    最近更新 更多