如何从模态UIView推送UIViewController?

时间:2017-04-12 08:11:28

标签: uiview swift3 uinavigationcontroller

我有一个如下所示的模态UIView,但是我无法找到从UIView推送另一个UIViewController的方法。视图的父UIViewController确实有一个UINavigationController。以下所有代码尝试都失败了。 UIView有一个UICollectionView

父UIViewController

startActivity(intent);
finish();

模态UIView

let searchView = SearchView()

view.addSubview(searchView)

1 个答案:

答案 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)