Reputation: 1418
I have a modal UIView created like below but I am unable to find a way to push another UIViewController from the UIView. The view's parent UIViewController does have a UINavigationController. All the code attempts below fail. The UIView has a UICollectionView
parent UIViewController
let searchView = SearchView()
view.addSubview(searchView)
modal UIView
class SearchView: UIView,
UICollectionViewDelegateFlowLayout,
UICollectionViewDataSource {
var collectionView: UICollectionView!
----
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
let mainStoryBoard = UIStoryboard(name: "Main", bundle: nil)
let myViewController: MyViewController = mainStoryBoard.instantiateViewController(withIdentifier: “MyViewController") as! MyViewController
// let currentController = self.getCurrentViewController()
// currentController?.navigationController?.pushViewController(myViewController, animated: true)
// self.parentViewController.navigationController.pushViewController(myViewController, animated: true)
let vc = self.window?.rootViewController
let navController = vc?.navigationController
navController?.pushViewController(myViewController, animated: true)
Upvotes: 0
Views: 329
Reputation: 72410
You can create one instance property of type UIViewController
with your SearchView
. Now use this property to pushViewController
. After that initialized it where you are creating instance of SearchView
.
class SearchView: UIView,
UICollectionViewDelegateFlowLayout,
UICollectionViewDataSource {
var collectionView: UICollectionView!
var vc: UIViewController?
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
let mainStoryBoard = UIStoryboard(name: "Main", bundle: nil)
let myViewController = mainStoryBoard.instantiateViewController(withIdentifier: "MyViewController") as! MyViewController
vc?.navigationController?.pushViewController(myViewController, animated: true)
}
Now where you are adding this SearchView
as subview set the vc property of its to self
.
let searchView = SearchView()
searchView.vc = self
view.addSubview(searchView)
Upvotes: 1