How to "cancel" a UIStoryBoardSegue

Fabio B. picture Fabio B. · Oct 19, 2011 · Viewed 31.6k times · Source

Does anyone know how to "stop" a segue transition conditionally:

My table view cells represent products which can be viewed in a drill-down "detail" view... or cannot! (It depends on a couple of things)

Now my App considers all products "unlocked":

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    NSIndexPath *selectedRowIndex = [self.tableView indexPathForSelectedRow];
    ListaProdottiController *prodottiViewController = [segue destinationViewController];
    prodottiViewController.blocco = [self.fetchedResultsController objectAtIndexPath:selectedRowIndex];
}

How can I cancel the row selection => drilldown, at this point?

Answer

Joshcodes picture Joshcodes · Mar 18, 2013

If you are targeting iOS 6 or greater, then my knowledge of the cleanest way to do this is the following:

-(BOOL)shouldPerformSegueWithIdentifier:(NSString *)identifier sender:(id)sender
{
    if([identifier isEqualToString:@"show"])
    {
        NSIndexPath *selectedRowIndex = [self.tableView indexPathForSelectedRow];
        Blocco *blocco = [self.fetchedResultsController objectAtIndexPath:selectedRowIndex];
        return [blocco meetRequiredConditions];
    }
    return YES;
}

Where there is a method

-(BOOL) meetsRequiredConditions;

Defined on your Blocco class returns YES if the "couple of things" which permit a drill-down are valid.