Data pass tableview cell to tab bar view controller in Swift
In your code
let ImageStoryboard = UIStoryboard(name: "Main", bundle: nil)
let ImageController = ImageStoryboard.instantiateViewController(withIdentifier: "IMAGESCOL") as! ImagesCollectionViewController
ImageController.RecivedData1 = media.bannerImages
These codes do nothing is expected, because you declare an ImageController
, but you don't use/present it.
So, according to the context, I assume that you have a UITabBarController
in Main.storyboard
, and the UITabBarController
has two UIViewController
, and one is ImageController
. If I were right, you could try codes below:
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let tabBarController = storyboard.instantiateViewController(withIdentifier: "IMAGEVID") as! UITabBarController
// You should access the `imageController` through your `tabBarController`,
// not instantiate it from storyboard.
if let viewControllers = tabBarController.viewControllers,
let imageController = viewControllers.first as? ImageController {
imageController.recivedData1 = media.bannerImages
}
navigationController?.pushViewController(tabBarController, animated: true)
Another suggestion is you should use camel case to name your properties. For more details, you can refer to this style guideline.
How to pass data between view controller and the tab-bar's view controllers using segue?
I got my answer
@IBAction func submitButonTapped(_ sender: Any) {
performSegue(withIdentifier: "homePage", sender: self)
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
let barViewControllers = segue.destination as! UITabBarController
let destinationViewController = barViewControllers.viewControllers![0] as! ViewControllerB
destinationViewController.containsData = Data
}
How to pass data from view controller to first tab of tab bar controller
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:-
- Just embed DiscoveryNewsViewController in navigation controller
2.Remove segue connection between DiscoveryNewsViewController and tabbarController
Pass data to Tab Bar Item (ViewController)
If you configured correctly such storyboard, you should get the reference to the UITabBarController
through prepare(segue)
, doing so:
private weak var tabBarViewController:UITabBarController?
// be sure to add prepare inside your segue manager (the container in this case)
override public func prepare(for segue: UIStoryboardSegue, sender: Any?) {
self.tabBarViewController = segue.destination as? UITabBarController
}
besides you may want to declare an enum to have a clean access to the embedded view controllers:
enum TabType:Int {
case blue
case green
}
thus you might define a function for injecting the selected indexPath, doing something like:
func inject(indexPath:IndexPath, into tab:TabType) {
if let greenVc = tabBarViewController?.viewControllers?[tab.rawValue] as? GreenViewController {
greenVc.selectedIndexPath = indexPath
tabBarViewController?.selectedIndex = tab.rawValue // this will auto select the TabType viewcontroller
}
}
finally whenever you have to change the current selection, you may call inject with the new indexPath:
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
self.inject(indexPath: indexPath, into: .green)
}
Note
Call instantiateViewController
means making a new "fake" view controller, which is a wrong approach, since you already have it embedded in your UITabBarController.viewControllers
pass data from tabbar controller to view controller in swift and xcode
Take a look at the documentation for the tab bar controller, in particular the viewControllers
property.
That property is an array of UIViewController
s, in the order they appear in the tab bar, so you can pick the one you need (viewControllers[0]
from your screen shot), cast it to your specific view controller subclass and then pass it your data.
Pass data from tableview to view controller in tab bar in Swift
Try this
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if let destination = (segue.destination as? UITabBarController)?.viewControllers.first as? EventViewController {
destination.event = events[(tableView.indexPathForSelectedRow?.row)!]
}
}
Pass data from tableview to tab bar view controller in Swift.
I have found a solution:
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "toTabController" {
var tabBarC : UITabBarController = segue.destinationViewController as UITabBarController
var desView: CaseViewController = tabBarC.viewControllers?.first as CaseViewController
var caseIndex = overviewTableView!.indexPathForSelectedRow()!.row
var selectedCase = self.cases[caseIndex]
desView.caseitem = selectedCase
}
}
Easy explanation: You need to get to your Tab bar controller and pick his view controllers. It normally has two, so you can pick the first. After that, define which class it belongs to, and you can pass your data just as you did before.
Pass Data From ViewController to TabBarController
The segue destination is the tabBar access it then you can get the first vc and pass the data
let custMainVC = segue.destination as! UITabBarController
let res = custMainVC.viewControllers!.first as! CustomerMainViewController
res.tempName = self.emailTextField.text!
Related Topics
Uibezierpath Subclass Initializer
Mirror Not Working in Swift When Iterating Through Children of an Objective-C Object
Expand Uitextview and Uitableview When Uitextview's Text Extends Beyond 1 Line
How to Make Playground Execution Time Is as Fast as If We Run in iOS Application
How to Change Status Bar Text Color Immediately in Swift in iOS 13
Wkwebview Is Showing Only Background
Scrolling in Uicollectionview Selects Wrongs Cells - Swift
Is Weak Self Needed for Table View Cell Button Closure
No Such Module "Sinch" Xcode 9.1 Swift 4
How to Update a Sent Message in Quickblox iOS
Routing Between Points with Mapbox
Xcode 9: Swift Dependency Analysis Error
Swift Bridging Header File Won't Work with Use_Frameworks
Is There Really No Way to Style Sklabelnode
Mysterious "Cryptographic Verification Failure" Error on MACos Sierra, Xcode 8
Custom Table View Row Action (Image)