SWRevealViewController使用swift以编程方式从菜单中打开第二个视图控制器

问题描述

我在我的应用程序中使用SWRevealViewController。除了一件事,一切都很好。

假设我的SWRevealViewController菜单中有A,B,C 3个项目。我想使用swift以编程方式打开B项。

更新:代码

 let storyboard = UIStoryboard(name: "Main",bundle: nil)
        let sw = storyboard.instantiateViewController(withIdentifier: "Reveal") as! SWRevealViewController
        let destinationController = storyboard.instantiateViewController(withIdentifier: "VehicleList") as! VehicleList
        let navigationController = UINavigationController(rootViewController: destinationController)
        sw.pushFrontViewController(navigationController,animated: true)

解决方法

您可以使用frontViewController的{​​{1}}属性访问主屏幕控制器,如下所示:

SWRevealViewController

您可以使用let navigationController = revealViewController().frontViewController as? UINavigationController navigationController?.pushViewController(viewController,animated: false) 的{​​{1}}函数来替换frontViewController,例如:

setFront(_:animated:)
,

这是我的问题的完整解决方案。

let storyboard = UIStoryboard(name: "Main",bundle: nil)
        let destinationController = storyboard.instantiateViewController(withIdentifier: "VehicleList") as! VehicleList
        let navigationController = revealViewController().frontViewController as? UINavigationController
        navigationController?.pushViewController(destinationController,animated: false)