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 between View Controllers in UITabBarController?
No, You can not pass the data between two ViewController directly, if they are connected to TabbArController.
If you want you can manage through NotificationCenter (So when a new change occur and you post the notification, it will automatically refresh the data)
NotificationCenter.default.addObserver(
self,
selector: #selector(self.getData),
name: .refreshAttendanceForSelectedDate,
object: nil)
@objc func getData() {
/// perform task in view controller
}
// post the changes when you get response ....
NotificationCenter.default.post(name: Constants.Notification.refreshAttendanceForSelectedDate, object: nil)
Or
You can pass the updated data by DataStorgae in a variable of that respective model type (LocalCache) by creating a singleton object. This variable will reside in memory till the app will reside in memory. After forcing kill the app or flush out the app from memory, this variable will automatically be removed.
Note: If needed, please don't forget to set value nil. (Eg. on logout action)
class DataStorage {
/// Created the singleton objectbas
static let instance = DataStorage()
/// Created the properties
var notificationbadgeCount: Int?
var configData: ConfigurationModel?
}
Use by set
DataStorage.instance.selectedStaffId = model.id
get the value
if let id = DataStorage.instance.selectedStaffId {
/// Perform respective task ....
}
Transfer data from tab bar controller to ViewController
What you want to do is, add a property called number
, or whatever you like, to your ViewController
class.
class ViewController: UIViewController {
var number: Int!
}
Then in tabBarController's viewDidLoad
, loop through the view controllers, check if they are of type ViewController
, and then set their number
property to the index of the tabBarController's view controllers:
class TabBarController: UITabBarController {
override func viewDidLoad() {
super.viewDidLoad()
for (index,vc) in (viewControllers!.enumerated())! {
if let viewController = vc as? ViewController {
vc.number = index
}
}
}
override func tabBar(_ tabBar: UITabBar, didSelect item: UITabBarItem!) {
if item == (self.tabBar.items as! [UITabBarItem])[0]{
print(1)
}
else if item == (self.tabBar.items as! [UITabBarItem])[1]{
print(2)
}
}
}
Unless, are you wanting to set the value whenever the tab is selected?
Best practice when sharing data/models between tabs of a TabBarController in Swift
let tabBar = tabBarController as! ExampleTabsController
But what if your ExampleViewController's tabBarController will be other UITabBarController or nil? Your architecture will stop working.
Your ExampleTabsController and FirstController are coupled very hard.
I can suggest you a little fix:
Add reference to your model inside FirstViewController:
var model: ExampleModel?
Inject it in your ExampleTabsController's loadViewControllers() function:
firstViewController.model = self.exampleModel
Now your ExampleTabsController and FirstViewController are not coupled.
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.
Related Topics
Nsdatecomponents Weekofyear Returns Wrong Date
Swift UI MACos Background Transparent Textfield
Controlling Size of Texteditor in Swiftui
How to Avoid That My Swift Async Method Runs on the Main Thread in Swiftui
Realitykit as a Framework to Build 3D Nonar Apps
Swift: Using "/" Slash in Filename with Createdirectoryatpath
How to Find the Operator Definition in Swift
What Happens When You Run Deprecated Code in Swift
How to Assign an Array to a Class Property by Reference Rather Than a Copy
Any Way to Chain == and || Operands
Executing Text-To-Speech in Order
Cannot Invoke 'Filter' with an Argument List of Type '((_) -> _)'
Swift Mutable Set: Duplicate Element Found
Swift- Variable Not Initialized Before Use (But It's Not Used)