在Xcode中切换viewControllers

时间:2012-04-11 11:00:01

标签: xcode storyboard viewcontroller

我正在写一个闪屏。启动画面将连接到服务器并接收一些数据。成功接收数据后,我想以编程方式转到下一个viewcontrller。我怎样才能做到这一点?它与按钮点击不一样?因为即使把代码放在我的LoadingViewContrller的viewDidLoad中,我也不会转发到我的下一个屏幕。

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
}
}

1 个答案:

答案 0 :(得分: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;
}
}

在计时器访问的方法中(在本例中为“updateTime”),执行以下操作:

-(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的声明。