Reputation:
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**
Upvotes: 2
Views: 1082
Reputation: 936
First of all you are doing two different things
there is a segue between DiscoveryNewsViewController and tabbarController
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:-
2.Remove segue connection between DiscoveryNewsViewController and tabbarController
Upvotes: 5
Reputation: 391
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
Upvotes: 0
Reputation: 393
so replace
self.navigationController?.pushViewController(tabBar, animated: true)
with
self.present(tabBar, animated: true, completion: nil)
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()
Upvotes: 0