How to load and save the alpha of a UIButton?
How can I save and load the UIButton's alpha values 开发者_JS百科in my iPhone app? I want to do this the easiest way possible. I have tried using various methods, but none have worked for me so far.
Try this:
CGFloat alpha = myButton.layer.opacity;
You might need to #import <QuartzCore/QuartzCore.h>
.
Edit (From this SO Question):
Then, you can simply store it in NSUserDefaults
:
[[NSUserDefaults standardUserDefaults] setObject:myButton.backgroundColor forKey:@"buttonColor"];
[[NSUserDefaults standardUserDefaults] synchronize]; // Thanks @Shah for reminding us about this!
Then retrieve it like such:
myButton.backgroundColor = [[NSUserDefaults standardUserDefaults] objectForKey:@"buttonColor"];
And when you're done with it:
[[NSUserDefaults standardUserDefaults] removeObjectForKey:@"buttonColor"];
NSUserDefaults Class Reference
You might want to have a look at the NSUserDefaults
.
// Setting a value
[[NSUserDefaults standardUserDefaults] setFloat:VALUE forKey:KEY];
[[NSUserDefaults standardUserDefaults] synchronize]; // This line is required to persist all changes make to the User Defraults
// Getting a value
[[NSUserDefaults standardUserDefaylt] floatForKey:KEY];
精彩评论