2012-04-05 46 views
12

在我的故事板,我有一個視圖作爲啓動畫面。在這個視圖中,我已經有了一個像「打開應用程序」這樣的按鈕,該按鈕用模態漸變打開菜單視圖。但是我也想讓screen自動執行segue,就像2秒後出現view一樣。如何執行與延遲Segue

一些代碼在這裏:

- (void)viewDidAppear:(BOOL)animated 
{ 
    [self performSegueWithIdentifier:@"splashScreenSegue" sender:self]; 
} 

正如你所看到的,我已經使用performSegueWithIdentifier但它會立即執行。有沒有辦法讓它延遲?

在此先感謝。

回答

25

您可以使用GCD的dispatch_after執行出現視圖2秒後您的SEGUE代碼,e.x:

- (void)viewDidAppear:(BOOL)animated 
{ 
    [super viewDidAppear:animated]; 

    double delayInSeconds = 2.0; 
    dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayInSeconds * NSEC_PER_SEC)); 
    dispatch_after(popTime, dispatch_get_main_queue(), ^(void){ 
     [self performSegueWithIdentifier:@"splashScreenSegue" sender:self]; 
    }); 
} 

此外,請確保您還記得重寫UIViewController的生命週期方法時要調用父類的實現。

+0

嗯很簡單,呵呵?沒有意外^^這段代碼令我困惑:[self performSelector:@selector(showSplash)withObject:nil afterDelay:SPLASH_HIDE_DELAY];我想知道,如果有一種方式適合賽車。謝謝。 – kubilay 2012-04-05 09:00:06

5

我認爲有一個更好的方法可以讓控制器自己處理調度問題。你可以這樣實現它:
首先創建一個像這樣的方法,以後當你想顯示另一個視圖控制器使用當前視圖控制器內該行的代碼,你會用它選擇

- (void)showAnotherViewController{ 
    [self performSegueWithIdentifier:@"yourSegueToAnotherViewController" sender:self]; 
} 

然後:

[self performSelector:@selector(showAnotherViewController) withObject:nil afterDelay:yourDelayInSeconds];