swift 如何通过点击按钮函数调用标签栏视图控制器

how to call tab bar view controller by click button function in swift

我有一个 common tab bar view controller。我已经将 3 other view controllers 连接到 navigation bar,我将这 3 个视图控制器连接到主视图控制器 tab bar view controller。所以现在我有 3 个带有 3 个视图控制器的标签栏项目。

1.Home 2. Cart 3. feedback

这很好!!

现在我的 first view controller(这是我的第一个 tab bar view controller)有一些 table view datadetail view controller。在我的 detail view controller 我有一个名为“Go to cart”的按钮。

所以实际流程是当用户按下 Go to cart 时。它必须移动到购物车视图 controller.Now 它正在移动。

但问题是。我无法看到我的标签栏项目和其他项目。但是如果我对每个标签栏项目都正常,它会显示所有标签栏项目。

当我从详细视图按钮转到我的 Cart 标签栏视图控制器时。然后没有标签栏项目显示出来。

我像演示一样做了所有这些 prototype.Like 我从按钮拖到购物车选项卡栏视图控制器导航栏。

但是它没有显示标签栏 why.Please 帮助我 out.I 没有任何单一的解决方案可以解决 this.I 我正在使用 swift 2.2.

谢谢。

已更新:

所以我需要编写这样的代码,对吗?但我不知道如何处理这段代码:

 let barViewControllers = sender.destinationViewController as! UITabBarController

    let nav = barViewControllers.viewControllers![1] as! UINavigationController

    let destinationViewController = nav.topViewController as! CartVC

但是我在第一行就崩溃了:

fatal error: unexpectedly found nil while unwrapping an Optional value
(lldb) 

请帮帮我

这是与您相似的故事板。

现在从按钮 "go to cart" 中删除 segue,它类似于我故事板中的 "show second tab" 按钮

改为按如下方式为其提供操作,

 @IBAction func showSecondTab(sender: AnyObject) {
            let  navController = self.tabBarController?.viewControllers![1] as! UINavigationController
            ///secondviewcontroller in your case is cart
            let secondViewController = navController.viewControllers[0] as! SecondViewController
            //set values you want to pass
            //lets say I want to pass name to secondVC
            secondViewController.name = "ABCD"

            self.tabBarController?.selectedIndex = 1
    }

您可以在 secondVC(CartVC) 的 viewDidLoad 中打印和检查值。