如何从当前标签栏加载第二个标签栏

时间:2017-12-29 04:00:56

标签: ios swift uitableview uitabbarcontroller

我在tabbarController中有表视图控制器。问题是当我在tableView中的一行中单击时它没有从现有标签栏控制器加载第二个标签栏控制器。

这是我的didSelectRowAt代码:

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

        let image = sneakersnews[indexPath.row].image
        DVC.getImage = image
        let news = sneakersnews[indexPath.row].news
        DVC.getNews = news
        self.navigationController?.pushViewController(tabBar, animated: true)
    }

您可以从此链接下载完整代码: 链路。https://drive.google.com/file/d/10w9MQaRxGlMSG_lK07N1c7nJWQLC8BWc/view

2 个答案:

答案 0 :(得分:0)

您必须将新tabBar显示为模式而不是您拥有的导航控制器

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

    let image = sneakersnews[indexPath.row].image
    DVC.getImage = image
    let news = sneakersnews[indexPath.row].news
    DVC.getNews = news

    present(tabBar, animated: true, completion: nil)
}

答案 1 :(得分:0)

在推送UITabBarController之后,您可以设置其选定的索引

self.navigationController?.pushViewController(tabBar, animated: true)
 // when you want to open first index
   tabBar.selectedIndex = 0
   // when you want to open second index
   tabBar.selectedIndex = 1