There seems to be a way to use AppDelegate
as one of the methods of sharing data between screens.
There are various ways to share data, but I think it's a relatively simple method.
I actually tried it, so I will write it as an article.
It is a file that can be accessed from all screens. Therefore, data can be shared by defining variables here.
This time I would like to use TabBarController
to share data between screens.
The data entered in the textField
of the blue view is passed to the yellow textField
with the save button.
Prepare a variable in AppDelagate
.
The button processing is as follows.
Prepare the constant delegate
to access AppDelegate and put the process to access the constant.
I want to access the following code, so set let delegate = UIApplication.shared.delegate as! AppDelegate
.
The variable you want to retrieve is in the AppDelegate type, so use as! To convert it to the AppDelegate type and retrieve it. (I'm not confident in the explanation around here)
class AppDelegate: UIResponder, UIApplicationDelegate {
var shareData:String = ""
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
return true
}
// MARK: UISceneSession Lifecycle
func application(_ application: UIApplication, configurationForConnecting connectingSceneSession: UISceneSession, options: UIScene.ConnectionOptions) -> UISceneConfiguration {
// Called when a new scene session is being created.
// Use this method to select a configuration to create the new scene with.
return UISceneConfiguration(name: "Default Configuration", sessionRole: connectingSceneSession.role)
}
func application(_ application: UIApplication, didDiscardSceneSessions sceneSessions: Set<UISceneSession>) {
// Called when the user discards a scene session.
// If any sessions were discarded while the application was not running, this will be called shortly after application:didFinishLaunchingWithOptions.
// Use this method to release any resources that were specific to the discarded scenes, as they will not return.
}
}
Then, put the characters entered in the textField into the variable prepared in AppDelegate
.
@IBAction func buttonAction(_ sender: Any) {
let delegate = UIApplication.shared.delegate as! AppDelegate
delegate.shareData = blueTextField.text!
}
YellowViewController
uses viewWillAppear
to display the data every time it switches.
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
let delegate = UIApplication.shared.delegate as! AppDelegate
yellowTextField.text = delegate.shareData
}
import UIKit
class BlueViewController: UIViewController {
@IBOutlet var blueTextField: UITextField!
@IBAction func buttonAction(_ sender: Any) {
let delegate = UIApplication.shared.delegate as! AppDelegate
delegate.shareData = blueTextField.text!
}
}
import UIKit
class YellowViewController: UIViewController {
@IBOutlet var yellowTextField: UITextField!
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
let delegate = UIApplication.shared.delegate as! AppDelegate
yellowTextField.text = delegate.shareData
}
}