Sorting Segues Sanely

There are three kinds of segues, push, modal and custom. You’ll probably most often use the ‘push’ method. It is incredibly useful in making storyboards do stuff.

Primarily there are two ways to invoke a segue. One is to rig up a button in Interface Builder that triggers a segue. The other is to invoke the segue on your own. I think that a lot of times I need to validate what’s on the screen before I can release it to the next controller. Therefore I need to do it myself.

Also you need to pass information back and forth, which will be taken care of by setting variables in the method:

-(void) prepareForSegue:(UIStoryBoardSegue *)segue sender:(id)sender {
//...
}


I’m not going to go into how to make the segue connections in Interface Builder. That much is obvious. I want to have control of the Storyboards!

This starts with naming your segues so we can trigger them when we want. I used @”nameThatSegue”

 

//The code that will trigger our segue is this:
[self performSegueWithIdentifier:@"nameThatSegue" sender:self];

Yes, now we can validate our form or whatever and then perform the segue in code. If you want to pass some info to this new viewController you can’t, but you can in the prepareForSegue: function, so we’ll catch it right there.

-(void) prepareForSegue:(UIStoryBoardSegue *)segue sender:(id)sender {
//You could have any number of segues, but we want this one
  if([[segue identifier] isEqualToString:@"nameThatSegue"]){
    DetailViewController *detailView = segue.destinationViewController;
    detailView.someVariable = self.varToPass; 
  }
}

Now getting information back from a view controller that pops itself off will require a delegate, which I’ll go into some other time.