如何使用ESTabBarController执行动作时推送控制器

时间:2019-03-07 10:23:31

标签: ios swift

我已经使用ESTabBarController实现了标签栏。到目前为止,我已经设法使中间选项卡启动模式,然后单击“添加”,我的应用程序将创建一个新条目。我正在努力的是在输入后立即过渡到Google Analytics(分析)控制器。

这是我的代码。它位于AppDelegate的{​​{1}}中。

didFinishLaunchingWithOptions

我尝试了

let tabBarController = ESTabBarController() tabBarController.delegate = self let v1 = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "QueryViewController") as! QueryViewController let v2 = QueryViewController() let v3 = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "SettingsViewController") as! SettingsViewController v1.tabBarItem = ESTabBarItem.init(TabBarContentView()) v2.tabBarItem = ESTabBarItem.init(TabBarContentView()) v3.tabBarItem = ESTabBarItem.init(TabBarContentView()) v1.tabBarItem.image = UIImage.init(named: "ic_home") v2.tabBarItem.image = UIImage.init(named: "ic_new") v3.tabBarItem.image = UIImage.init(named: "ic_settings") v1.tabBarItem.selectedImage = UIImage.init(named: "ic_home") v2.tabBarItem.selectedImage = UIImage.init(named: "ic_new") v3.tabBarItem.selectedImage = UIImage.init(named: "ic_settings") let n1 = NavigationController.init(rootViewController: v1) let n2 = NavigationController.init(rootViewController: v2) let n3 = NavigationController.init(rootViewController: v3) tabBarController.shouldHijackHandler = { tabbarController, viewController, index in if index == 1 { return true } return false } tabBarController.didHijackHandler = { [weak tabBarController] tabbarController, viewController, index in DispatchQueue.main.asyncAfter(deadline: .now() + 0.2) { let alert = UIAlertController(title: "New", message: nil, preferredStyle: UIAlertController.Style.alert) let addQuery = UIAlertAction(title: "Add", style: .default) { (alertAction) in let textField = alert.textFields![0] as UITextField let newQueryID = UUID().uuidString QueryModel().createNewQuery(id: newQueryID, name: textField.text ?? "") let viewController = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "QueryAnalyticsController") as! QueryAnalyticsController; viewController.selectedQueryID = newQueryID // Then push that view controller onto the navigation stack //tabBarController?.navigationController?.pushViewController(viewController, animated: true) //tabBarController?.present(viewController, animated: true) } alert.addTextField { (textField) in textField.placeholder = "Title" } alert.addAction(addQuery) let cancel = UIAlertAction(title: "Cancel", style: .cancel, handler: nil) alert.addAction(cancel) tabBarController?.present(alert, animated: true, completion: nil) } } tabBarController.viewControllers = [n1, n2, n3] self.window?.rootViewController = tabBarController

可以工作,但没有考虑NavigationController,我需要它才能返回。

我也尝试过

tabBarController?.present(viewController, animated: true)

但是它并没有像我期望的那样推送viewController。

1 个答案:

答案 0 :(得分:0)

替换

self.window?.rootViewController = UINavigationController(rootViewController:tabBarController)

使用

let nav = tabBarController!.selectedViewController as! UINavigationController

nav.pushViewController(viewController, animated: true)

由于您当前的导航为零

  

tabBarController?.navigationController? <<<<<无

或像孩子一样推动

{{1}}