2012-04-11 63 views
2

我在寫一個啓動畫面。啓動畫面將連接到服務器並接收一些數據。當數據成功接收後,我想以編程方式轉到下一個viewcontrller。我怎樣才能做到這一點?它與按鈕點擊不一樣?因爲即使將代碼放入我的LoadingViewContrller的viewDidLoad中,我也不會轉發到下一個屏幕。在Xcode中的viewControllers之間切換

TableViewController *tvc = [[TableViewController alloc] init]; 
tvc.modalTransitionStyle = UIModalTransitionStyleCrossDissolve; 
[self presentModalViewController:tvc animated:NO]; 

我希望在檢索完所有數據後自動跳轉到TableViewContrller。

以下是我從網絡中檢索數據時的代碼。

- (void)fetchedData:(NSData *)responseData { 
if(responseData == nil){ 
    [ErrorViewController showError]; 
}else{ 
//methods to start parsing and adding json into array 
if(delegate){ 
    [delegate jsonReceivedData:array]; 

//codes to go to next screen should be here 
} 
} 
+0

您是否有辦法檢查所有數據何時被檢索?如果是這樣,請發佈該代碼,我會告訴你如何去做。 – AMayes 2012-04-11 15:54:05

+0

@Amayes我編輯了代碼 – Hexark 2012-04-12 01:47:09

回答

2

好的,這樣做很簡單。使您的應用的第一個屏幕與啓動屏幕相同。聲明並實例一個NSTimer,可能在viewWillAppear中,像這樣:

mainTimer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(updateTime) userInfo:nil repeats:YES]; 

然後實例化一個BOOL:

- (void)fetchedData:(NSData *)responseData { 
if(responseData == nil){ 
    [ErrorViewController showError]; 
}else{ 
//methods to start parsing and adding json into array 
if(delegate){ 
    [delegate jsonReceivedData:array]; 

    myBool = YES; 
} 
} 

在你的計時器訪問的方法(在這種情況下,「更新時間」),請執行下列操作:

-(void)updateTime{ 
    if(myBool){ 
     [mainTimer invalidate]; 
     MyViewController *vC = [[MyViewController alloc] init]; 
     //pass vC information, now that it has been initialized 
     //or pass information to a singleton, from which vC can retrieve it 
     // (I can show you how to do that, too, if need be) 
     //I will assume you are using a navigationController 
     [self.navigationController pushViewController:vC animated:YES]; 
    } 
} 

對於空間的緣故,我要離開了的mainTimermyBool的聲明。

+0

假設「啓動畫面」是指應用程序的初始屏幕。 – AMayes 2012-04-12 15:24:25