Xcode Swift How-to: Update / Reload the View

So I had this problem where I had to update another view after a user logged in, had to change the interface and so on in Xcode programming with Swift an app.

I Googled like one hour to find the solution.

Finally the solution is called viewDidLoad() vs. viewWillAppear() 

The first one, viewDidLoad() is only called once in the program and if you unload it on purpose

However the second one, viewWillAppear() is called EVERYTIME the UI is being displayed.

So from there you can check the variable and update / reload for example the button:

override func viewWillAppear(animated: Bool) {
        var isloggedin = NSUserDefaults.standardUserDefaults().boolForKey("isloggedin")
        if (isloggedin == true) {
            LoginBut.title = "Log Out"
        } else {
            LoginBut.title = "Sign In"
        }
    }

 

  1. I don’t care about this, now i’m making around $3000 a month.
    There is tricky method i found on the internet.
    If you want to learn it too, simply type in google

  2. $3000/month is nothing. LOL

  3. Hey Alex. I had a question about this code. I have a login view controller and once the login is successful I wanted to change the title of my button like what you have but my button is on a different my main view controller. I tried implementing your code and Im not quite sure how to make it work when a user logs in.

Leave a Comment