Hexark
Hexark

Reputation: 413

Switching between viewControllers in Xcode

I am writing a splash screen. The splash screen would connect to the server and receive some data. When the data has been received successfully, I want to programmatically go to the next viewcontrller. How can I achieve this? Its not the same as button click? Because I dont get forwarded to my next screen even when I put the code in my viewDidLoad of my LoadingViewContrller.

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

I would like to jump to TableViewContrller automatically after all my data has been retrieved.

Below are my codes when I am retrieving data from the network.

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

Upvotes: 2

Views: 1437

Answers (1)

AMayes
AMayes

Reputation: 1767

Okay, the way to do this is simple. Make the first screen for your app the same as the splash screen. Declare and instantiate an NSTimer, possibly in viewWillAppear, like so:

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

Then instantiate a 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;
}
}

In the method accessed by your timer (in this case "updateTime") do the following:

-(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];
    }
}

For the sake of space, I'm leaving out the declarations of mainTimer and myBool.

Upvotes: 2

Related Questions