Matt Jacquet Matt Jacquet - 3 months ago 15
Swift Question

nsuserdefaults in viewdidload disable hidding views

I'm using nsuserdefaults to save some bools data but I'm facing problem:
in my storyboard, I checked 'hidden' for a view, but when I use my application, the view is not 'hidden' and i noticed it's because my userdefaults in the

viewdidload:
when i delete it, the view is hidden.
Is there a way to set it hidden?
i tried in
viedidload
to set hidden but don't work.
here's my code:

override func viewDidLoad() {
super.viewDidLoad()
// viewnote2.hidden = true
let boolsKey = NSUserDefaults.standardUserDefaults()
viewnote2.hidden = boolsKey.boolForKey("bool2")
}


that's all about this view

Answer

Some time change on UI doesn't work on viewDidLoad, try to hide the view on viewDidAppear. Also check that you have set the Bool value for bool2 key in UserDefaults.

If you haven't set the Bool value in UserDefaults than you need to set first like this.

let boolsKey = NSUserDefaults.standardUserDefaults()
boolsKey.setBool(false,forKey:"bool2")
boolsKey.synchronize()
Comments