Search code examples
swiftsegueviewcontroller

label disappears when I launch app the second time


I have a main.storyboard with three Viewcontroller. In the first viewcontroller there is a start button with a perform.segue method to the second viewcontroller. In the second viewcontroller there is a textfield which asks for the name of the user.

When the user types in their name and clicks on a continue button the textfield.text (name of the person) is put into a label on the third viewcontroller by the prepare(for segue) method. This procedure happens when I launch my app for the very first time.

When I launch my app for the second time the thirsViewcontroller is shown without the "welcome procedure" of the first- and the secondviewcontroller, but the label disappears.

code of the secondViewController

@IBAction func continueButton(_ sender: Any) {
        animateButton()
        
        if enterYourNameTextfield.text != "" {
        DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 0.8, execute: {
            self.performSegue(withIdentifier: "thirdScreenSegue", sender: self)
       
            }
        )}
    }
    
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        let thirdController = segue.destination as! thirdViewController
        thirdController.myString = "Hello \(enterYourNameTextfield.text!)! How are you today ?"
    
    }

Code of the thirdViewcontroller

@IBOutlet weak var helloNameLabel: UILabel!
var myString = String()

code of the AppDelegate.swift file

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool  {
    let isFirst = UserDefaults.standard.bool(forKey: "isLaunched") // edited this after rmaddy's comment
    var viewControllerWithIdentifier = "initialView"
    
        if !isFirst {
    UserDefaults.standard.set(true, forKey: "isLaunched")
    viewControllerWithIdentifier = "firstView"
    }
    let mainStoryboard : UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
    let initialViewController : UIViewController = mainStoryboard.instantiateViewController(withIdentifier: viewControllerWithIdentifier) as UIViewController
    self.window = UIWindow(frame: UIScreen.main.bounds)
    self.window?.rootViewController = initialViewController
    self.window?.makeKeyAndVisible()
    sleep(2)
        
        return true
    }

Solution

  • In secondViewController you need to persist the name of the user. You can persist the user name in multiple ways but for your use case you can save the name in the user defaults as shown below :

     override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
            let name = enterYourNameTextfield.text ?? ""
            UserDefaults.standard.set(name, forKey: "name")
        }
    

    in your thirdViewController's viewDidLoad method you can retrieve the user name as shown below

    override func viewDidLoad(){
       let name = UserDefaults.standard.string(forKey: "name") ?? ""
       myString = "Hello \(name)! How are you today ?"
    }