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
.
My code so far:
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 {
}
}
}
I am getting error on this line :
self.contentSubView.addSubview(allCollectionViewController)
Cannot convert value of type 'firstvc' to expected argument type 'UIView'
How do i solve this issues.
Thanks in advance !!
UPDATED:
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)
}
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
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
you can push present viewcontroller but you can't add viewcontroller as subview.
If you want to add as subview then you can do like,
or push viewcontroller if you have navigation controller like
or present it like
do like
The error is very obvious. You can only use addSubview according to this definition:
If you want to use multiple view controllers in one view controller, I would recommend using container view controllers.
Try this tutorial: https://cocoacasts.com/managing-view-controllers-with-container-view-controllers/
Otherwise a simple approach is to use:
You can then use constraints on the view to manage multiple view controllers.