我有一个如下所示的模态UIView,但是我无法找到从UIView推送另一个UIViewController的方法。视图的父UIViewController确实有一个UINavigationController。以下所有代码尝试都失败了。 UIView有一个UICollectionView
父UIViewController
startActivity(intent);
finish();
模态UIView
let searchView = SearchView()
view.addSubview(searchView)
答案 0 :(得分:1)
您可以使用UIViewController
创建一个SearchView
类型的实例属性。现在将此属性用于pushViewController
。之后,将其初始化为您创建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)
}
现在,您要将此SearchView
添加为子视图,将其vc属性设置为self
。
let searchView = SearchView()
searchView.vc = self
view.addSubview(searchView)