How to “cancel” a UIStoryBoardSegue

前端 未结 9 1832
小蘑菇
小蘑菇 2020-12-23 21:40

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

相关标签:
9条回答
  • 2020-12-23 22:02

    Here another solution I just found.

    In your Mainstoryboard TableView, as you use automatic segue from your cell (identifier=Cell) to the destination view, you can also add another cell with another identifier (identifier=CellWithoutSegue). So when creating new cell in cellForRowAtIndexPath, just reuse the cell identifier you want (with or without segue).

    Hope it helps!

    (Tell me if you want some code source examples).

    Regards,

    0 讨论(0)
  • 2020-12-23 22:09

    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.

    0 讨论(0)
  • 2020-12-23 22:11

    The easiest solution is to create manual segue in story board and use that as seen below.

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

    Or


    @Fabio: I was trying to get a solution for same kind of use-cases and I almost found a solution.

    Use-cases 1. Stop segue transition conditionally 2. Change destination viewController conditionally

    Solution:

    Use "Custom" segue. Follow below steps to create Custom segue 1. Create a subclass of UIStoryboardSegue "MyCustomSegue.h"

    @interface MyCustomSegue : UIStoryboardSegue
    @end
    

    "MyCustomSegue.m"

    Override initWithIdentifier for implementing use-case 1 and 2 If you return nil, segue will be cancelled/no action will be taken You instantiate your ViewController and set that as a destination. You can set destination as your old xib file also.. that code is commented, but I ensured that will work.

    @implementation MyCustomSegue
    - (id)initWithIdentifier:(NSString *)identifier source:(UIViewController *)source destination:(UIViewController *)destination{
    
        UIStoryboard *storyBoard= [UIStoryboard storyboardWithName:@"MainStoryboard_iPhone" bundle:nil];
    
        UIViewController *viewController = [storyBoard instantiateViewControllerWithIdentifier:@"testIdentifier"];
    
        // MyViewController* viewController= [[MyViewController alloc]initWithNibName:@"MyViewController" bundle:nil];
        return [super initWithIdentifier:identifier source:source destination:viewController];
    }
    
    1. You must override "perform".

    You can implement use-case 1 here also..

    - (void)perform {
        // if either source or destination is nil, stop
        if (nil == self.sourceViewController || nil == self.destinationViewController) return;
        // return; //No Action. Segue will be cancelled
        UINavigationController *ctrl = [self.sourceViewController navigationController];
        [ctrl
         pushViewController:self.destinationViewController
         animated:YES];
    }
    

    Hope this helps. Plz write if you are not clear.

    0 讨论(0)
提交回复
热议问题