Swift:从其他视图控制器切换选项卡和视图控制器

时间:2018-06-21 12:32:20

标签: ios swift uitableview uitabbarcontroller

enter image description here

我有一个正在使用SideMenu的侧面菜单。可以从ViewController1和ViewController 2调用侧面菜单。我希望能够:

  • 使用侧面菜单切换标签
  • 从侧面菜单中按下ViewController3和ViewController4,也可以通过按下按钮1或2(我已经使VC2上的按钮起作用)来按下
  • 展示ViewController3或ViewController4之后,我希望能够按回去并返回ViewController2

什么是最好的方法?当我尝试在MenuViewController的tabBarController?.selectedIndex = 1方法中使用didSelectRowAt时,由于tabBar为nil,因此无法正常工作。我也找到了这个答案:

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
self.dismiss(animated: true) {
    let topVC = self.topMostController()
    let tabBar = self.storyboard!.instantiateViewController(withIdentifier: "tabBarController") as! UITabBarController
    tabBar.selectedIndex = indexPath.row
    topVC.present(tabBar, animated: false, completion: nil)
}

}

func topMostController() -> UIViewController {
    var topController: UIViewController = UIApplication.shared.keyWindow!.rootViewController!
    while (topController.presentedViewController != nil) {
        topController = topController.presentedViewController!
    }
    return topController
}

这有点用,但我认为在这种情况下不正确。

0 个答案:

没有答案
相关问题