iPhone Development 101
This domain is for sale.

iPhone 101

Code Tips



Looking for a tutorial on how to use Storyboards? Click here.

Passing Data With prepareForSegue

If your views/scenes are connected via segues instead of IBActions, here's how to pass data from one view controller to the next using prepareForSegue.

Your segue must have a unique identifier (in the Storyboard editor, click on the segue and go to the attributes inspector).

Here's an example for a table view cell segue (this replaces the didSelectRow code for the table view delegate):

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender 
    if ([[segue identifier] isEqualToString:@"ShowSelectedFont"]) 
        NSIndexPath *indexPath = [self.theTableView indexPathForSelectedRow];
        Game *game = [games objectAtIndex:indexPath.row];

        GameScreenController *controller = [segue destinationViewController];
        controller.game = game;

Here's a simpler example for a UIButton click segue:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender 
    if ([[segue identifier] isEqualToString:@"showWebPage"]) 
        WebViewController *controller = [segue destinationViewController];
        controller.pageToLoad = @"fnord.html";

Triggering a Segue Programmatically

[self performSegueWithIdentifier: @"SegueToScene1" sender: self];

Instantiating a View Controller from the Storyboard

If you have a scene on the storyboard with no segues attached to it, you can instantiate it by using its storyboard identifier:

UIStoryboard *storyboard = self.storyboard;
MyViewController *controller = [storyboard 
[self.navigationController pushViewController:controller animated:YES];

Additional References