ios - 如何在 collectionview 单元格中的 didselect 上显示不同的 View Controller

标签 ios swift uicollectionview

我有 3 个 View Controller ,分别是 firstvcsecondvcthirdvc。我有一个水平滚动的 Collection View 。我已经做到了。如果我选择任何单元格,它会打印它是哪个索引路径。没关系,没问题。所以在我的 mainviewcontroller 中,我有一个水平滚动的 Collection View 。并且有一个名为 myviewUIView。每当我按下任何单元格时,我都会得到它的 indexPath。我需要在我的 mainviewcontroller 中将我的 3 个 View Controller 显示为 myview 的 subview 。

到目前为止我的代码:

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
    // handle tap events
    print("You selected cell #\(indexPath.item)!")
    if indexPath.item == 0 {
         let alertStoryBoard =  UIStoryboard(name: "Main", bundle: nil)
         if let allCollectionViewController = alertStoryBoard.instantiateViewController(withIdentifier:"firstvc") as? firstvc {
             self.contentSubView.addSubview(allCollectionViewController)
         } else if indexPath.item == 1 {
            
         } else if indexPath.item == 2 {
            
         }
    }
}

我在这一行遇到错误:

 self.contentSubView.addSubview(allCollectionViewController)

无法将“firstvc”类型的值转换为预期的参数类型“UIView”

我该如何解决这个问题。

提前致谢!!

更新:

如果 indexPath.item == 0 {

    let alertStoryBoard =  UIStoryboard(name: "Main", bundle: nil)
    
    
    if  let allCollectionViewController = alertStoryBoard.instantiateViewController(withIdentifier:"firstvc") as? firstvc  {
        
       
        
          self.contentSubView.addSubview(allCollectionViewController.view)
        

    
    
} else if indexPath.item == 1 {
        
        
        let alertStoryBoard =  UIStoryboard(name: "Main", bundle: nil)
        
        
        if  let allCollec = alertStoryBoard.instantiateViewController(withIdentifier:"secondvc") as? secondvc  {
       
            self.contentSubView.addSubview(allCollec.view)

    
}else if indexPath.item == 2 {
       
            let alertStoryBoard =  UIStoryboard(name: "Main", bundle: nil)
            
            
            if  let wController = alertStoryBoard.instantiateViewController(withIdentifier:"Thirdvc") as? Thirdvc  {
       
                self.contentSubView.addSubview(wController.view)

}

只单独显示第一个 vc 类,不显示第二个和第三个。另外,为什么我要关注 lk 这意味着。当我按下任何 Collection View 单元格时,该特定类 View Controller 必须显示在我的 mainviewcontroller

的 uiview subview 中

在我的第一个 View Controller 中,我放置了一个带有一些背景颜色的 uiview。但它根本没有显示。它显示的是白色。那也没有正确显示

最佳答案

您可以推送当前的 View Controller ,但不能将 View Controller 添加为 subview 。

如果你想添加为 subview ,那么你可以这样做,

  self.contentSubView.addSubview(allCollectionViewController.view)

或者如果你有像这样的导航 Controller ,则推送 viewcontroller

 navigationController?.pushViewController(allCollectionViewController, animated: true)

或者像这样呈现

 present(allCollectionViewController, animated: true) { 

    }

关于ios - 如何在 collectionview 单元格中的 didselect 上显示不同的 View Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46237221/

相关文章:

ios - 如何在后台触发多步api交易?

ios - 从 firebase 获取数组图像

location - iOS 的 kCLLocationAccuracyHundredMeters 常量

ios - 如何根据 ADFS 3.0 识别通过 ADAL 身份验证的用户?

ios - CAShapeLayer 改变不同画笔的宽度

ios - Swift:是否可以从堆栈中删除 View Controller?

ios - ARKit -[UIView setAnimationsEnabled :] Performing any operation from a background thread on UIView or a subclass is not supported

ios - 具有相同数据源的多个 UICollectionView

ios - 我们可以在后台启动 iBeacon 发射器吗?

ios编程-在应用程序级别覆盖静态库中的静态库