Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to "cancel" a UIStoryBoardSegue

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?

like image 990
Fabio B. Avatar asked Oct 19 '11 10:10

Fabio B.


4 Answers

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.

like image 92
Joshcodes Avatar answered Nov 18 '22 04:11

Joshcodes


I don't know if it is the right way to do it but I discovered a workaround.

From the storyboard I associate(control+click) a segue from the status bar in the view controller. Give the segue an ID (for example: switchSegue).

Now, from an action in your code (in my code I use a button), I call:

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

That way you can control if your segue is performed or not. Try tutorials that helped me from here and here

Hope this helps.

like image 30
igbopie Avatar answered Nov 18 '22 04:11

igbopie


I am using an much easier and tidy approach.

Storyboard

  1. Create two identical cell with different identifiers. Eg: "cellWithSegue" and "cellWithoutSegue".
  2. Connect the first cell ("cellWithSegue") with the segue you want to display.
  3. Do not connect the second cell with any segue.

Table View

  1. On cellForRowAtIndexPath, implement a logic to determine if the cell should be linked a segue or not.
  2. For cells that should be linked with the segue use the "cellWithSegue" identifier, for the rest the "cellWithoutSegue".

This way looks a lot easier to implement and also does not alter the way segues are supposed to work.

like image 18
zirinisp Avatar answered Nov 18 '22 02:11

zirinisp


I may be wrong here, but after struggling myself with this, I just disabled the cell's user interaction on the cells where I didn't want the seque triggered (in cellForRowAtIndexPath:). Seems to work perfectly, and it's only 1 line of code!

cell.userInteractionEnabled = NO;
like image 14
SomaMan Avatar answered Nov 18 '22 02:11

SomaMan