我有3個視圖控制器,稱爲firstvc
,secondvc
,thirdvc
。我有一個水平滾動的集合視圖。我已經做到了。如果我選擇任何單元格,它將打印它是哪個索引路徑。沒關係,沒問題。所以在我的mainviewcontroller
我有一個集合視圖將水平滾動。還有一個叫myview
的UIView
。每當我按下任何單元格時,我都會得到它的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顏色。這也顯示不正確
使用像'self.contentSubView.addSubview(allCollectionViewController.view)' –
@ Anbu.Karthik請參閱我的更新後 – david
設置根據您的MyView的 –