如何"取消"UIStoryBoardSegue

Fab*_* B. 43 iphone storyboard uitableview ios ios5

有没有人知道如何有条件地"停止"segue过渡:

我的表格视图单元格表示可以在深入"详细"视图中查看的产品......或者不能!(这取决于几件事)

现在我的应用程序认为所有产品都"解锁":

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

此时如何取消行选择=>向下钻取?

Jos*_*des 79

如果您的目标是iOS 6或更高版本,那么我对最干净的方法的了解如下:

-(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;
}
Run Code Online (Sandbox Code Playgroud)

哪里有方法

-(BOOL) meetsRequiredConditions;
Run Code Online (Sandbox Code Playgroud)

如果允许向下钻取的"几件事"有效,则在Blocco类上定义返回YES.

  • 这应该是接受的答案.谢谢!! (4认同)
  • 我同意 - 这应该是公认的答案! (3认同)

igb*_*pie 32

我不知道这是否是正确的方法,但我发现了一种解决方法.

从故事板中我将视图控制器中的状态栏关联(控制+单击)一个segue.给segue一个ID(例如:switchSegue).

现在,从您代码中的操作(在我的代码中我使用一个按钮),我调用:

 [self performSegueWithIdentifier:@"switchSegue" sender:sender];
Run Code Online (Sandbox Code Playgroud)

这样你可以控制你的segue是否被执行.尝试从这里这里帮助我的教程

希望这可以帮助.

  • 投票支持Raywederlich.com教程的链接.:) (5认同)
  • 这是我发现的最简单的方法:)很酷!谢谢! (2认同)

zir*_*isp 18

我正在使用一种更简单,更整洁的方法.

故事板

  1. 创建两个具有不同标识符的相同单元 例如:"cellWithSegue"和"cellWithoutSegue".
  2. 将第一个单元格("cellWithSegue")与要显示的segue相连.
  3. 不要将第二个单元连接到任何segue.

表视图

  1. 在cellForRowAtIndexPath上,实现一个逻辑来确定单元格是否应该链接到segue.
  2. 对于应与segue链接的单元格,使用"cellWithSegue"标识符,其余为"cellWithoutSegue".

这种方式看起来更容易实现,也不会改变segues的工作方式.


Som*_*Man 14

我可能在这里错了,但在努力解决这个问题之后,我只是禁用了单元格上的用户交互,我不希望触发seque(在cellForRowAtIndexPath :)中.似乎工作得很好,而且它只有一行代码!

cell.userInteractionEnabled = NO;
Run Code Online (Sandbox Code Playgroud)


小智 11

最简单的解决方案是在故事板中创建手动segue并使用如下所示.

[self performSegueWithIdentifier:@"loginSuccessSegue" sender:self];
Run Code Online (Sandbox Code Playgroud)

要么


@Fabio:我试图获得相同用例的解决方案,我几乎找到了解决方案.

用例1.有条件地停止segue转换2.有条件地更改目标viewController

解:

使用"自定义"segue.按照以下步骤创建自定义segue 1.创建UIStoryboardSegue"MyCustomSegue.h"的子类

@interface MyCustomSegue : UIStoryboardSegue
@end
Run Code Online (Sandbox Code Playgroud)

"MyCustomSegue.m"

覆盖initWithIdentifier以实现用例1和2如果返回nil,则将取消segue /不执行任何操作您实例化ViewController并将其设置为目标.您可以将目标设置为旧的xib文件..该代码已被注释,但我确保它可以正常工作.

@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];
}
Run Code Online (Sandbox Code Playgroud)
  1. 您必须覆盖"执行".

你也可以在这里实现用例1 ..

- (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];
}
Run Code Online (Sandbox Code Playgroud)

希望这可以帮助.Plz写道,如果你不清楚.

  • 这似乎是需要的额外工作.根据选择的表格单元格选择要调用(或不调用)哪个序列会不会更容易?您不会创建自定义segue,您的视图控制器将失去对其他视图控制器的依赖.您可以使用以下命令请求在代码中执行seque:[self performSegueWithIdentifier:@"SegueIdentiferName"sender:self] ;. (3认同)