2013-03-22 198 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

相關問題