问题
How would I allow the app to save & load the current state of a button (i.e. if it is hidden, how would I save this, so that next time it will be hidden)? Also, I have it currently set so that when a button is pressed, all the other buttons become un-hidden. How can I set it so that certain ones don't become un-hidden if they have been set un-hidden in NSUserDefaults if you see what I mean?
Thanks
回答1:
You can check with Apple's documentation on NSUserDefaults to get any additional information you need.
NSUserDefaults *defaults = [NSUserDefaults standardDefaults];
[defaults setObject:myBool boolForKey:@"hiddenButton"];
[defaults synchronize];
Then you can just pull it in the same way and set your hidden value.
NSUserDefaults *defaults = [NSUserDefaults standardDefaults];
myButton.hidden = [defaults boolForKey:@"hiddenButton"];
EDIT:
This would be one way to implement, but it really depends on what you want.
myViewController.h
BOOL myButtonState;
myViewController.m
-(void)viewDidLoad {
NSUserDefaults *defaults = [NSUserDefaults standardDefaults];
myButton.hidden = [defaults boolForKey:@"hiddenButton"];
}
-(IBAction)buttonPressed {
myButtonState = TRUE;
NSUserDefaults *defaults = [NSUserDefaults standardDefaults];
[defaults setObject:myButtonState boolForKey:@"hiddenButton"];
[defaults synchronize];
}
来源:https://stackoverflow.com/questions/7322442/xcode-check-if-uibutton-is-hidden-using-nsuserdefaults