Passing Data Use User Dafault (viewController 1 --> ViewContoller 3)
This time we have problem is "How to passing data from first view controller to third view controller", now this my answer. take a look.
Above have 3 picture "FirstController -> SecondController -> ThirdController" and use this code UserDafault
- FirstController
import UIKit
class PassUserDafaultViewController: UIViewController {
@IBOutlet weak var textFieldUser: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
}
@IBAction func passingDataUser(_ sender: Any) {
let userDefault = UserDefaults()
userDefault.set(textFieldUser.text, forKey: "saveDataRecive")
}
}
- SecondController
import UIKit
class SecondNotifViewController: UIViewController {
@IBOutlet weak var notifLabel: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
}
@IBAction func saveData(_ sender: UIButton) {
let vc = P2ViewController()
navigationController?.pushViewController(vc, animated: false)
}
}
- ThirdController
import UIKit
class P2ViewController: UIViewController {
@IBOutlet weak var labelPass: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
}
override func viewWillAppear(_ animated: Bool) {
let userDefault = UserDefaults()
if let data = userDefault.object(forKey: "saveDataRecive") {
if let message = data as? String {
self.labelPass.text = message
}
}
}
}
This my note as my problem and solution @luffyselah
No comments: