按下indexPath单元格时如何导航或显示其他视图控制器

时间:2016-03-17 20:35:12

标签: ios iphone swift uicollectionview

我有一个2 cells的集合视图。因此,默认情况下,第一个单元格将为0,1......而且我还有2 viewcontroller。命名为1. firstVC, 2. secondVC。我需要的是,当我点击单元格1时,它应该将firstVC显示为modal视图segue

就像我按第二个单元格一样,它应该将secondVC显示为modal视图segue ..

如何做到这一点,我可以打印我选择的单元格。如何显示viewcontrollers ??

   func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
        // You can use indexPath to get "cell number x", or get the cell like:
        let cell = collectionView.cellForItemAtIndexPath(indexPath)

    if((indexPath.row) == 0) {
        print("Layout 1 clicked")

    }
    else if((indexPath.row) == 1) {
        print("Layout 2 clicked")
    }
    else if((indexPath.row) == 2) {
        print("Layout 3 clicked")
    }
    else if((indexPath.row) == 3) {
        print("Layout 4 clicked")
    }


    }

请帮帮我......谢谢!

1 个答案:

答案 0 :(得分:2)

使用self.presentViewController(UIViewController, animated: Bool)或在Interface Builder中添加segue并使用self.performSegueWithIdentifier(String, sender: AnyObject?)进行调用。 另外,当在IB中添加segue时,如果需要,请确保将其更改为“以模态方式呈现”。

相关问题