简体   繁体   中英

How to pass data from view controller to first tab of tab bar controller

I want to pass image and news from view controller to connected first tab of tabbar controller .The first tab the NewsViewController is not displaying the news title and image. didSelectRowAt indexPath method as follow.

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=sneakersnews[indexPath.row].image
    DVC.getNews=sneakersnews[indexPath.row].news
    self.navigationController?.pushViewController(tabBar, animated: true)
}

How to pass and display the news and image on first tab NewsViewController from the table view?

You can download the project from this link:

https://drive.google.com/file/d/1qfI3UaXoUTzOS6Jfe40wj99NAifVkz5Y/view?usp=sharing **

First of all you are doing two different things

  1. there is a segue between DiscoveryNewsViewController and tabbarController

  2. You are pushing tabbar controller like this

     let DVC = tabBar.viewControllers?[0] as! NewsViewController //this is not required as it will always open UIViewController at Zero index so comment this //tabBar.selectedIndex = 0 let image = sneakersnews[indexPath.row].image DVC.getImage = image let news = sneakersnews[indexPath.row].news DVC.getNews = news self.navigationController?.pushViewController(tabBar, animated: true) 

Please read commented lines in above code

So you cannot do both above mention points at a time because a segue connection is already pushing your TabBarController and your self.navigationController?.pushViewController is also pushing TabBarController.

SOLUTION:-

  1. Just embed DiscoveryNewsViewController in navigation controller

2.Remove segue connection between DiscoveryNewsViewController and tabbarController

在此处输入图片说明

  1. You shouldnt push a tabbar into navigation controller. So, remove the segue in storyboard between DiscoveryNewsViewController and TabBar

so replace

self.navigationController?.pushViewController(tabBar, animated: true)

with

self.present(tabBar, animated: true, completion: nil)
  1. Since you are not refreshing your assigned value to image and label in UI so its not displaying. To do that just create a function

    func refreshData() { newsTitle.text = getNews NewsImage.image = getImage }

in NewsViewController and call it just after

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

like this

DVC.refreshData()

I saw your code, and get the problem and many test!

Not a complex problem but need take care next time.

It's because you connect a segue from your cell to a TabbarController, and when you tab the cell, it will automatic goto the tabbarController due to the segue.

and you init another tabbarController and pass the data then present it. you make a breakpoint in your NewsViewController viewDidLoad and can see hit twice.

There are many other problems in your code, In my opinion, you can only use the segue to pass data, To make your code stable and predictable

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM