如何显示不同的视图控制器上选择了collectionv视图单元格

时间:2017-09-15 10:17:38

标签: ios swift uicollectionview

我有3个名为firstvcsecondvcthirdvc的视图控制器。我有一个水平滚动的集合视图。我做到了。如果我选择任何单元格,它将打印它的索引路径。没关系,没问题。所以在我的mainviewcontroller我有一个水平滚动的集合视图。还有一个UIView称为myview。每当我按任何单元格时,我都会得到它indexPath。我需要在我的myview中将我的3个视图控制器显示为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)

Cannot convert value of type 'firstvc' to expected argument type '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这意味着。当我按下任何集合视图单元格时,该特定的类视图控件必须在我的mainviewcontroller

的子视图中显示uiview

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

4 个答案:

答案 0 :(得分:1)

您可以推送当前的viewcontroller,但不能将viewcontroller添加为子视图。

如果您想添加为子视图,那么您可以这样做,

  self.contentSubView.addSubview(allCollectionViewController.view)
如果你有像

这样的导航控制器,请按

或推送viewcontroller

 navigationController?.pushViewController(allCollectionViewController, animated: true)

或表示

 present(allCollectionViewController, animated: true) { 

    }

答案 1 :(得分:1)

//Do this:

let storyboard = UIStoryboard(name: "MyStoryboardName", bundle: nil)
let controller = storyboard.instantiateViewController(withIdentifier: "someViewController")
self.present(controller, animated: true, completion: nil)

//You have to present your view controller and what you are doing is adding it as a sub view.

答案 2 :(得分:0)

错误非常明显。您只能根据此定义使用addSubview:

open func addSubview(_ view: UIView)

如果要在一个视图控制器中使用多个视图控制器,我建议使用容器视图控制器。

试用本教程:https://cocoacasts.com/managing-view-controllers-with-container-view-controllers/

否则一个简单的方法是使用:

self.contentSubView.addSubview(allCollectionViewController.view)

然后,您可以在视图上使用约束来管理多个视图控制器。

答案 3 :(得分:0)

喜欢

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)
}
相关问题