First off, let me explain my app and its flow. The app opens, and the user creates a profile (stores all the data in a dictionary of type [String: Any]). After the user clicks on create, it sends them to a Console Screen (which displays parts of the information the user input, such as their name through a segue). Theres a tab that lets them EDIT their profile (name, weight, address, etc). When the user edits their info (to change their name, weight, etc), it should also update the info displayed on the Console Page.
My main question is, how do I structure it so the data is universally available to all of the tab views. More specifically, so the data is NOT an INSTANCE or copy of the data. I've created a class called Person which has all of the variables stored in it (firstName, lastName, etc). I've tried declaring the instance of the person class as a variable before the UIViewController class declaration, but it won't let me use the UITextField .text property (because they havent been created yet...?).
Here is my Person class...
class Person {
var firstName: String
var lastName: String
var phoneNumber: String
var weight: Int
var gender: String
var streetAddress: String
var cityState: String
init(firstName: String, lastName: String, phoneNumber: String, weight: Int, gender: String, streetAddress: String, cityState: String) {
self.firstName = firstName
self.lastName = lastName
self.phoneNumber = phoneNumber
self.weight = weight
self.gender = gender
self.streetAddress = streetAddress
self.cityState = cityState
}
}
I have also subclassed the UITabBarController as TabBarViewController because I've read that you can send the data to that subclassed viewController and make it universally available to all of the imbedded tabs. I'm just at a loss as to how it all works.
I apologize for possibly asking a repeat question...I have look/researched this and I haven't found any suitable solutions for my particular case.
Updated code to save data as CoreData...
guard let appDelegate =
UIApplication.shared.delegate as? AppDelegate else {
return
}
let managedContext = appDelegate.persistentContainer.viewContext
let userInfo = PInfo(firstName: firstNameField.text, lastName: lastNameField.text, cityState: cityStateField.text, streetAddress: streetAddressField.text, gender: genderInputField.text, userWeight: 200)
save(withPersonInfo: userInfo, withContext: managedContext)
I'm able to access the data in various viewControllers, but it gives it back to me as a single item array... Ideally, I'd like to have them as key:value pairs so I can easily set them to display and are editable in the Edit Profile viewController.
Thanks!
One solution would be
Delegates. The class where you are getting the data would sets your delegate, and anyone else who wishes to get that subscribe to that delegate.As matter of fact: I think using
Delegate + CoreDatamight save you from troubles; Listen for data then saved that data. Now your other controllers will do a quick fetch (core data is so fast man:) for that one user scenario )Some code: