Alex Cio
Alex Cio

Reputation: 6052

prepareForSegue doesn't init new view

i have been looking at several turorials and all call the new view with just one line of code or more if they pass any information to the next view. At the moment i want to build a menu with segue like this:

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {          
    NSIndexPath *path = [self.tableView indexPathForSelectedRow]; 
    if ([path row]==0) {
        MainViewController *mtvc = (MainViewController *)[segue destinationViewController];
    } else {
        SettingsViewController *svc = [segue destinationViewController];
    }       
}

Normaly all examples use "segue.identifier isEqualToString:@"" ". But i couldn't connect the cell of my table view inside the storyboard with more than one other view. Therefore i used the selected row to identifie which of my views should segue.

I don't know if this could occure the problem but i initialized the tableview from my splashview in this way:

-(void)turnNext {
    UIStoryboard *storyboard = self.storyboard;
    MenuTableViewController *mtvc = [storyboard instantiateViewControllerWithIdentifier:@"Menu"];
    [self presentViewController: mtvc animated:YES completion:nil];
}

Upvotes: 0

Views: 715

Answers (2)

Krishna Datt Shukla
Krishna Datt Shukla

Reputation: 997

You should use segues for this purpose.

  • Create 2 custom segues on your Source controller in storyboard .
  • Set their identifier as 'segueToMainViewController' and 'segueToSettingsViewController'

Now, in tableView - 'didselectrowAtIndexPath' method use following.

//use your logic here

if(indexPath.row == 1) {
   [self performSegueWithIdentifier:@"segueToMainViewController" sender:nil];
}
else {
   [self performSegueWithIdentifier:@"segueToSettingsViewController" sender:nil]
}

You can Pass data as sender if you need to paas anything to your controllers.

Upvotes: 1

Martol1ni
Martol1ni

Reputation: 4702

-(void)turnNext {
    UIStoryboard *storyboard = self.storyboard;
    MenuTableViewController *mtvc = [storyboard instantiateViewControllerWithIdentifier:@"Menu"];

    [self presentViewController: mtvc animated:YES completion:nil];
}
             ^^^^^^

change that to

[self.navigationController pushViewController:mtvc animated:YES];

Upvotes: 1

Related Questions