Home Ask Login Register

Developers Planet

Your answer is one click away!

Brucegrass February 2016

Where to place code to populate a UITextfield (title field) after a segue screen returns to main screen?

I am building a Swift - Single View app. I’ve added a “segue" view to allow user to store a title line preference using NSUserDefaults. When I return to the primary view the title line isn’t filled with the new title info from NSUserDefaults. I’ve placed the code to populate the main screen title line in the viewDidLoad section noted below. Strangely enough, if I go to the “segue" screen and back to main screen again without making changes then it shows up. Where can I place code to get the title line stored in my preferences segue screen to show up when I go back to the main view? Here is my code that works, but only after going back and forth from the seague twice. Thanks for any help.

override func viewDidLoad() {
     super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        let defaults = NSUserDefaults.standardUserDefaults()
        let titlePref = defaults.objectForKey("usertitleLineKey")!
        titleLine.text = titlePref as! String
    }

Answers


mohonish February 2016

You should use viewWillAppear or viewDidAppear callback methods to refresh any content in your view when or just before it shows up. viewDidLoad will only be called once when the view is initialised on to memory.


Anokrize February 2016

Try this code:

override func viewWillAppear() {
    let defaults = NSUserDefaults.standardUserDefaults()
    let titlePref = defaults.objectForKey("usertitleLineKey")!
    titleLine.text = titlePref as! String
}

Post Status

Asked in February 2016
Viewed 2,218 times
Voted 14
Answered 2 times

Search




Leave an answer


Quote of the day: live life