'Save configuration from class to class through save Button in a specific VC
I have a View Controller which has a UITextField and a button below it which i want to configure it when its pressed to save the data which are in the UITextField and store that data to the API class, Ill show some of the code and images below
So the default must be the URL that you are seeing but If the user decide to change that and press the button "Ruaj" then the URL needs to be updated in the APICaller class to, Ill show how the code is structured in the APICaller class where the URL needs to be updated
The code for the Settings class:
@IBOutlet weak var apiTextField: UITextField!
@IBOutlet weak var saveButton: UIButton!
@IBAction func theSaveButton(_ sender: Any) {
let alert = UIAlertController(title: "", message: "The configurations are being saved...", preferredStyle: .alert)
self.present(alert, animated: true, completion: nil)
Timer.scheduledTimer(withTimeInterval: 3, repeats: false) { _ in
guard let tabBarController = self.storyboard?.instantiateViewController(withIdentifier: "qrCode") else {
return
}
tabBarController.modalPresentationStyle = .custom
self.present(tabBarController, animated: true, completion: nil)
}
}
The APICaller class:
class APICaller {
let token = "NjQzODM2N0NDNDM4GSH350NjA03536GSE3E="
static let shared = APICaller()
var baseURL = "http://411.42.111.234:3050/api/" // the URL when save Button is pressed needs to update the baseURL here :)
func getVehicles(for id: String, completed: @escaping (Result<[Vehicles],OnTraErrors>) -> Void ){
let endpoint = baseURL + "GetVehicles/?UserIdentificationValue=\(id)"
I believe this might be very easy but for me as a new Swift wanabe Dev I am having difficulties like everyone who started out this journey but I am truly learning on daily basis through this amazing community!
Solution 1:[1]
Don't have each view controller keep a separate copy of the URL.
I suggest you set up a model object (a struct) that holds your app state, and pass that around between your view controllers. Do a Google search on the UDF (Unidirectional Data Flow) design pattern.
Alternately you could set up a data container singleton that you ask for persistent state, but that has issues and is generally not recommended.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Duncan C |
