2017-09-15 17 views
0

我有3個視圖控制器,稱爲firstvc,secondvc,thirdvc。我有一個水平滾動的集合視圖。我已經做到了。如果我選擇任何單元格,它將打印它是哪個索引路徑。沒關係,沒問題。所以在我的mainviewcontroller我有一個集合視圖將水平滾動。還有一個叫myviewUIView。每當我按下任何單元格時,我都會得到它的indexPath。我需要在我的mainviewcontroller中將我的3個視圖控制器顯示爲myview的子視圖。如何顯示不同的視圖控制器上選擇collectionv視圖單元格

到目前爲止我的代碼:

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這意味着什麼。當我按任何集合視圖單元格時,該特定的類視圖controlelr必須在我的子視圖中顯示我的mainviewcontroller

在我的第一個視圖控制器中,顯示。它顯示whitee顏色。這也顯示不正確

+0

使用像'self.contentSubView.addSubview(allCollectionViewController.view)' –

+0

@ Anbu.Karthik請參閱我的更新後 – david

+0

設置根據您的MyView的 –

回答

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) 
} 
+0

以供我參考我的項目zip https://www.dropbox.com/s/fwj745kdgqvgjxa/Collection%20view%20example.zip?dl=0 – david

+0

我也會檢查上面的一個解決方案 – david

+0

@spikesa - 我的答案將起作用,我檢查了示例項目 –

1

你可以推現在viewcontroller,但你不能添加viewcontroller作爲子視圖。

如果您要添加爲子視圖,那麼你可以不喜歡,

self.contentSubView.addSubview(allCollectionViewController.view) 

或推視圖 - 控制,如果您有導航控制器像

navigationController?.pushViewController(allCollectionViewController, animated: true) 

或呈現它像

present(allCollectionViewController, animated: true) { 

    } 
+0

請看我更新的帖子 – david

+0

@LionAny解決方案請https://stackoverflow.com/questions/46251141/swipe-left-or-right-to-load-the-view-controller-with -the-collection-view-cell-hi – david

0

錯誤非常明顯。您只能根據這個定義中使用addSubview:

open func addSubview(_ view: UIView) 

如果你想在一個視圖控制器使用多個視圖控制器,我會建議使用容器視圖控制器。

試試這個教程:https://cocoacasts.com/managing-view-controllers-with-container-view-controllers/

否則,一個簡單的方法是使用:

self.contentSubView.addSubview(allCollectionViewController.view) 

然後可以使用在視圖上約束來管理多個視圖控制器。

+0

請參閱我更新的帖子 – david

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. 
相關問題