Reputation: 11
I would like to set value to a bool variable which, after first click on a button makes it disappear. That button should be visible only on a fresh reinstall or an update. Right now I am using NSUserDefaults. But where do I set it the first time?
I am doing this in AppSettings init function but still it gets reset:
let defaults = NSUserDefaults.standardUserDefaults()
defaults.setBool(true, forKey: kFirstTime)
defaults.synchronize()
Inside cellForRowAtIndexPath:
if defaults.boolForKey(kFirstTime) == true
{
cell.newButton.hidden = false
}
else if defaults.boolForKey(kFirstTime) == false
{
cell.newButton.hidden = true
}
cell.configureCell()
I want the key to return false every time after clicking on it once
Upvotes: 0
Views: 180
Reputation: 1352
If you want to show the button when update, so the key in NSUserDefault should be related to the version of your APP.
if let value = defaults.boolForKey(keyWithVersion) {
// have value
} else {
// no value means first time enter this version of APP
}
Upvotes: 0
Reputation: 685
If you remove
defaults.setBool(true, forKey: kFirstTime)
and change
if defaults.boolForKey(kFirstTime) == true
to
if defaults.boolForKey(kFirstTime) == nil {
defaults.setBoolForKey(false, forKey: kFirstTime)
//etc
}
then that should work.
Rather than setting the value to true, you will then just be checking for an absence of any value, which will be the case for a fresh install.
To check for updates, you will need to expand your method to store something like the value of the last version used and then have something in your updated code to check for this as well as for the nil value to accommodate people who come in at versions other than version 1.0
Upvotes: 1