如何在选择 collectionv 视图单元格时显示不同的视图控制器 [英] How to show different view controller on did select of collectionv view cell

查看:23
本文介绍了如何在选择 collectionv 视图单元格时显示不同的视图控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有 3 个视图控制器,分别称为 firstvcsecondvcthirdvc.我有一个将水平滚动的集合视图.我已经这样做了.如果我选择任何单元格,它将打印它是哪个索引路径.没事,没问题.所以在我的 mainviewcontroller 我有一个将水平滚动的集合视图.还有一个 UIView 叫做 myview.每当我按下任何单元格时,我都会得到它的 indexPath.我需要在我的 mainviewcontroller 中将我的 3 个视图控制器显示为 myview 的子视图.

I have 3 view controllers called firstvc, secondvc, thirdvc. And I have one collection view which will scroll horizontally. I have done that. And if I select any cell, it will print which index path it was. It's fine, no problem. So in my mainviewcontroller I have one collection view which will scroll horizontally. And there is one UIView called myview. Whenever I press any cell, I get its indexPath. I need to show my 3 view controllers as subviews of myview in my mainviewcontroller.

到目前为止我的代码:

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"

我该如何解决这个问题.

How do i solve this issues.

提前致谢!!

如果 indexPath.item == 0 {

if 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 这意味着.当我按下任何集合视图单元格时,该特定类视图控制器必须显示在我的 mainviewcontroller

Only showing first vc class alone, not showing second and third one. Also, why i am following lk this means. When ever i press on any collection view cell, that particular class view controlelr have to show in my sub view of uiview in my mainviewcontroller

在我的第一个视图控制器中,我放置了一个带有一些背景颜色的 uiview.但根本没有显示.它显示的是白色.那也没有正确显示

In my first view controller i placed one uiview with some background color .But that not at all showing .Its showing whitee color.That too not showing correctly

推荐答案

喜欢

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
// handle tap events
print("You selected cell #\(indexPath.item)!")

    based on your comment remove the subviews before add on your myview

    for subview in contentSubView.subviews {
          subview.removeFromSuperview()
    }

    let alertStoryBoard =  UIStoryboard(name: "Main", bundle: nil)
    var controller: UIViewController!

if indexPath.item == 0 {

    if  let allCollectionViewController = alertStoryBoard.instantiateViewController(withIdentifier:"firstvc") as? firstvc  {

        controller = allCollectionViewController
      }

} else if indexPath.item == 1 {

        if  let allCollec = alertStoryBoard.instantiateViewController(withIdentifier:"secondvc") as? secondvc  {
       controller = allCollec
       }


}else if indexPath.item == 2 {


            if  let wController = alertStoryBoard.instantiateViewController(withIdentifier:"Thirdvc") as? Thirdvc  {
       controller = wController
    }

}

   addChildViewController(controller)

 // Add the child's View as a subview
  contentSubView.addSubview(controller.view)
  controller.view.frame = contentSubView.bounds
  controller.view.autoresizingMask = [.flexibleWidth, .flexibleHeight]

  // tell the childviewcontroller it's contained in it's parent
   controller.didMove(toParentViewController: self)
}

这篇关于如何在选择 collectionv 视图单元格时显示不同的视图控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆