mack mack - 4 months ago 41
Swift Question

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

I have one

common tab bar view controller
.And i have connected
3 other view controllers
with
navigation bar
and i connect this 3 view controllers with the main
tab bar view controller
.So now i have 3 tab bar items with 3 view controller.

that is
1.Home 2. Cart 3. feedback


This is fine !!

And now my
first view controller
(That is my first
tab bar view controller
) have some
table view data
and with
detail view controller
.And in my
detail view controller
i have one button called "
Go to cart
".

So the actual flows is when user press
Go to cart
.It have to move to cart` view controller.Now its moving.

But the problem is .I was not able to see my tab bar items and other items .But if i go normal to each tab bar items its showing all tab bar items.

When i go from my Detail view button click to my
Cart
tab bar view controller..Then no tab bar items are showing down.

I did all this like demo prototype.Like i drag from button to cart tab bar view controller navigation bar.

But its not showing tab bar why.Please help me out.I don't have any single solution to solve this.I am using swift 2.2.

Thanks.

enter image description here

Updated :

So i need to code some thing like this Right?.But i don't know how to handle this code:

let barViewControllers = sender.destinationViewController as! UITabBarController

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

let destinationViewController = nav.topViewController as! CartVC


But i am getting crash in first line :

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


Please help me out

Answer

enter image description here

Here is storyboard similar to yours.

Now remove segue from button "go to cart" which is similar to "show second tab" button in my storyboard

Instead provide action to it as follows,

 @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
    }

you can print and check values in viewDidLoad of secondVC(CartVC).

Comments