2013-03-22 195 views
2

我正在使用SVProgressHUD类(https://github.com/samvermette/SVProgressHUD),并且我得到了一个带有按钮的主视图控制器,该按钮通过使用另一个视图控制器的连接按钮进行连接。 在主视图控制器,我添加以下代码:Progress HUD不显示在正确的视图控制器上

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

我想要做的是,其他的视图控制器被加载的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需要消失。

回答

2

可以直接从按钮连接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]; 
             }); 
        }); 
} 
+0

我没有使用NSURLConnection,所以第二个选项对我来说会更好。我不需要等待连接,我需要等待第二个视图控制器(蓝色)。在那个课上,我从互联网下载了一些图像,所以如果我按下主视图控制器上的按钮,它需要显示加载HUD,并且在加载蓝色视图控制器图像后,HUD需要消失,而蓝色需要消失弹出。 – Shinonuma 2013-03-22 09:47:17

+0

问题是,你给“秒”,但我不知道下载所有图像需要多长时间... – Shinonuma 2013-03-22 09:48:35

+0

任何机会,你是否使用一些“框架”(例如ASIHTTPRequest)来下载你的图像?如果是这样,那么应该有一些方法或块,以便在连接完成后可以执行应该执行的操作。我的观点是,由于您正在下载图像,因此您应该关闭进度HUD并在下载后推入蓝屏;您不应该使用选项2,因为完成下载的时间可能会有所不同。 – neilvillareal 2013-03-22 12:32:07

相关问题