1
votes

I'm new to swift and I made two view controllers :

ViewController1 = Get value from textfield and update button text to match the textfield user saved with userdefaults on ViewController2.

ViewController 2 = Just a textfield for name and a button that save to user.defaults and dismiss the view.

For now everything works excepted the Button on ViewController1 doesn't update with viewDidLoad neither viewDidAppear or viewWillApear. I have to close application and task kill it then start app for it shows the new button name.

Can you please tell me how to change my button text when it's comming back to ViewController1 after dismiss done ?

let defaults = UserDefaults.standard

private var monNom: String = ""

@IBOutlet weak var compteApplication: UIButton!

override func viewDidLoad() {
    super.viewDidLoad()
    
    if let monNom = UserDefaults.standard.value(forKey: "Nom") as? String {
        self.monNom = monNom
        print(monNom)
        
       if (monNom != "") {
            
            let contentsNumber = defaults.string(forKey: "savedContentsNumber") ?? "0"
            
            print("I'm not empty")
            compteApplication.setTitle("Compte \(monNom) \(String(contentsNumber))", for: .normal)
            
            
            
        }
        
    }
1

1 Answers

1
votes

if it doesn't work for viewDidAppear also this means your vc's presentation is a modal , so change it to full screen or use a delegate to send the update