1
votes

I have a storyboard with tabbarcontroller. One of tab bar has a tableview and I want that when the user tap in a row from tableview open a detail view with another tabbar. The problem is when I open detail view tab bar hides . how to show tabbar at didselectrowatindexpath?

The code in didselectrowatindexpath:

   func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        let storybaord=UIStoryboard(name: "Main", bundle: nil)
        let DVC=storybaord.instantiateViewController(withIdentifier: "NewsViewController") as! NewsViewController
        DVC.getImage=sneaker[indexPath.row]
        DVC.getNews=News[indexPath.row]
        self.navigationController?.pushViewController(DVC, animated: true)

    }

I have created single view application.I have created DiscoveryNewsTableViewCell.I populated the table view cell with two arrays 1-sneaker 2- News array .I want when the particular array displayed ist tab of bar controller .But i did get these fields on first tab of tabbar controller .Here is the sample code for this https://drive.google.com/file/d/1uSYl-4a1UQXpMXkSQ_l8aij6EH_v2yna/view?usp=sharing

2
DetailView is in another tabbar? you mean , we have to open another tabBarviewController in which one of the viewController is detailView. Am I right? - Hemant Sabale

2 Answers

1
votes

Try modifying your code in this way

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
// setting these values in local storage using user defaults
    let currentObj = News[indexPath.row]
    UserDefaults.standard.set(currentObj, forKey: "currentOb")
    let storybaord=UIStoryboard(name: "Main", bundle: nil)
    let DVC=storybaord.instantiateViewController(withIdentifier: "NewsViewController") as! NewsViewController
    self.navigationController?.pushViewController(DVC, animated: true)
    }
0
votes

This is happening because you are pushing the viewController instead of TabBarController.

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    let storybaord=UIStoryboard(name: "Main", bundle: nil)
    let tabBar=storybaord.instantiateViewController(withIdentifier: "tabBar") as! UITabBarController
    let DVC = tabBar.viewControllers[0] as! NewsViewController
    tabBar.selectedIndex = 0
    DVC.getImage=sneaker[indexPath.row]
    DVC.getNews=News[indexPath.row]
    self.navigationController?.pushViewController(tabBar, animated: true)

}

In the above code replace 0 in

  tabBar.viewControllers[0]
  tabBar.selectedIndex = 0

with index of view controller you want to open.

And also in the line

   let tabBar=storybaord.instantiateViewController(withIdentifier: "tabBar") as! UITabBarController

tabBar has to be set as storyboard identifier for UITabBarController that you want to display as shown below.

enter image description here