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?
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.
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.
I am using an much easier and tidy approach.
Storyboard
Table View
This way looks a lot easier to implement and also does not alter the way segues are supposed to work.
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;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With