[Swift] Share data using AppDelegate

Introduction

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.

What is AppDelegate

It is a file that can be accessed from all screens. Therefore, data can be shared by defining variables here.

I tried using it

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. スクリーンショット 2020-12-20 11.53.12.png

Prepare a variable in AppDelagate. スクリーンショット 2020-12-20 10.57.33.png 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
    }

This code

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
    }
}

Recommended Posts

[Swift] Share data using AppDelegate
[Swift] switch statement using tuples
[Swift] Try using Collection View
Data processing using Apache Flink
[Swift] Processing to share screenshots
[Swift] Asynchronous processing using PromiseKit
[Swift] Implement UITableView using xib