shouldPerformSegueWithIdentifier and prepareForSegue in iOS 6

The prepareForSegue method is called just before a segue is performed and allows you to pass variables to the new view controller that is the segue’s destination.

While the prepareForSegue for segue call hasn’t changed between iOS 5 and iOS 6 there has been a new method, shouldPerformSegueWithIdentifier, that has been introduced in iOS 6 which allows you to specify if you want the segue to be be performed or not.. It’s very useful especially for things like only displaying one popover at a time
I’ve included an example of how to to use it below:

-(void) prepareForSegue:(UIStoryboardPopoverSegue *)segue sender:(id)sender
{
    if ([[segue identifier] isEqualToString:@"Next View"]) {
      
        //The segue is to the a View Controller called NextViewController
        (NextViewController *nvc =  (NextViewController  *) segue.destinationViewController;
       
       //Set properties in the view controller - note the views outlets are not set yet
        nvc.stringProperty1 = @"This Is Property 1";
      
         nvc.property2 = @"This Is Property 2";
       //Set self as the delegate
       nvc.delegate = self;
    }
}

- (BOOL)shouldPerformSegueWithIdentifier:(NSString *)identifier sender:(id)sender {
    

    
        if ([identifier isEqualToString:@"Crazy View" ]) {
           
            return NO;
      }


        return YES;
        
    }
        
        
       
}



2 thoughts on “shouldPerformSegueWithIdentifier and prepareForSegue in iOS 6

  1. chaaruu

    Thank you very much for this example..i was helpless for my code validation..but because of your this example i solved it..

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *


*