【问题标题】:How call a method when a UICollectionViewCell is pressed?按下 UICollectionViewCell 时如何调用方法?
【发布时间】:2012-10-14 22:52:49
【问题描述】:

我试图在按下 UICollectionViewCell 时调用另一个 UIViewController,但我就是不能。我应该使用以下哪种方法?

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{

}

-(void) collectionView:(UICollectionView *)collectionView performAction:(SEL)action forItemAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender
{

}

【问题讨论】:

    标签: xcode uiviewcontroller ios6 uicollectionview uicollectionviewcell


    【解决方案1】:

    只需将UICollectionViewCell 链接到情节提要中的新视图控制器! 你不需要这些方法。

    【讨论】:

    • 如果你有 AViewController 和 BViewController 你必须在你的 AViewController.m 中导入 BViewController.h 以使其工作。如果你这样做了,问题就出在其他地方,所以请仔细检查 segue。
    • 解决了。 segue 方法必须链接到 UICollectionView 上的 UIViewCell 然后添加下面的方法:-(void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { if ([segue.identifier isEqualToString:@"AbrirPerfilSegue"]) { CVCell *cell = (CVCell *)sender; PerfilViewController *perfil = (PerfilViewController *) [segue destinationViewController]; } }
    【解决方案2】:

    在一个简单的情况下,您可能想要使用情节提要并建立从您的单元到另一个视图控制器的转场。如果您想将每个单元格链接到同一个视图控制器类,那将起作用。 如果您需要实例化不同类的视图控制器,或者您不使用情节提要,那么您将使用您提到的第一个函数

    -(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-10-10
      • 2020-05-16
      • 2013-04-01
      • 2012-12-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多