【问题标题】:prepareForSegue never being calledprepareForSegue 永远不会被调用
【发布时间】:2013-11-03 17:07:38
【问题描述】:

我有一个由许多单元格组成的 UICollectionView。在确定这是适当的操作后,我希望能够点击其中一个单元格并让我的 Storyboard 转到另一个视图控制器。

我已经在我的 Storyboard 中创建了我的辅助视图控制器以及 segue。在我的 UICollectionView 子类中,我实现了以下...

-(BOOL)shouldPerformSegueWithIdentifier:(NSString *)identifier sender:(id)sender {
    return NO; // So that I can determine whether or not to perform the segue based on app logic
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    [collectionView deselectItemAtIndexPath:indexPath animated:YES];

    // Selection logic here
    [self performSegueWithIdentifier:@"showDetailView" sender:self];
}

在这个类中,我还实现了 -(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender 以允许我在执行 segue 之前设置我的详细视图。它看起来像这样:

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
   NSLog(@"HELLO, WORLD!!!");
}

一切似乎都井井有条,但我发现prepareForSegue 从未被调用过。我试过记录,设置断点。没有迹象表明这个方法被调用过。我在这里想念什么?为什么不调用此方法?虽然它现在有一个简单的“Hello, World”语句,但我之前有我的详细视图依赖的代码,它会导致异常,因为详细视图设置不正确。

这里还有什么我遗漏的吗?我应该做而我目前没有做的事情?

【问题讨论】:

    标签: ios objective-c storyboard uicollectionview segue


    【解决方案1】:

    您是在告诉 iOS 永远不要与此争论:

    -(BOOL)shouldPerformSegueWithIdentifier:(NSString *)identifier sender:(id)sender {
        return NO; // So that I can determine whether or not to perform the segue based on app logic
    }
    

    将 NO 更改为 YES,它应该可以工作。或者在其中添加逻辑,当你想要 segue 时至少返回 YES。

    【讨论】:

    • 如果你查看我的 didSelectItemAtIndexPath 方法,你会看到我正在手动执行 segue。
    • @Shadowman 您是否尝试将其设置为 YES 并查看它是否有效?
    【解决方案2】:

    如果您设置了shouldPerformSegueWithIdentifier:NO,则永远不会调用PrepareForSegue

    【讨论】:

      【解决方案3】:

      确保您使用的是您设置的原型单元(带有动态单元) 故事板中的标识符并在您的委托中使用它。我有这个问题 并花了我一点时间弄清楚。

      static NSString *cellIdent = @"prototypeCellName";
      UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdent];
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-10-27
        • 2013-10-12
        • 2012-03-27
        • 2013-08-29
        • 1970-01-01
        • 1970-01-01
        • 2015-05-06
        • 2015-10-15
        相关资源
        最近更新 更多