How would I save a UIButton#39;s properties and load with a button?(如何保存 UIButton 的属性并使用按钮加载?)
问题描述
可能重复:
如何我在应用中保存和加载 UIButton 的 alpha 值?
我想保存 UIButton 的状态(例如它的 alpha 值以及它是否隐藏),然后在用户退出并重新加载应用程序时加载.
I would like to save the state of the UIButton (e.g. its alpha value and whether it is hidden or not) and this would then load up when the user quits and reloads the app.
我用 NSUserDefaults 尝试了一些代码,但没有成功.
I've tried some bits of code with NSUserDefaults but with no luck.
有人可以提供一些示例代码,以便我可以保存和加载按钮的状态吗?
Could somebody help with some sample code so that I can save and load the button's state?
谢谢,
詹姆斯
推荐答案
与Shaharyar的回答有关(我不知道怎么评论):
Related to Shaharyar's answer (i don't know how to comment):
在这种情况下,您需要使用 NSNumber.
in this case you need to use NSNumber.
[[NSUserDefaults standardUserDefaults] setValue:[NSNumber numberWithFloat:SOME_FLOAT] forKey:KEY];
因为 float 不是一个对象,而 NSNumber 是一个.
because float is not an object, but NSNumber is one.
已
1) 确保在应用程序首次运行后创建默认值:在您的 AppDelegate 的 initialize 方法中:
1) To make sure your defaults are created after the application runs at first time:
in your AppDelegate's initialize-method:
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSDictionary *appDefaults = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithFloat:SOME_FLOAT], @"YOUR_KEY",
nil];
[defaults registerDefaults:appDefaults];
2) 更新后的默认值:
2) Updating defaults after:
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
[prefs setFloat:FLOAT_VALUE forKey:@"YOUR_KEY"];
[prefs synchronize];
3) 读取默认值:
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
float FLOAT_VALUE = [prefs floatForKey:@"YOUR_KEY"];
这篇关于如何保存 UIButton 的属性并使用按钮加载?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何保存 UIButton 的属性并使用按钮加载?
基础教程推荐
- 如何将图像从一项活动发送到另一项活动? 2022-01-01
- SwiftUI-ScrollViewReader的ScrollTo不滚动 2022-01-01
- iOS - UINavigationController 添加多个正确的项目? 2022-01-01
- UIImage 在开始时不适合 UIScrollView 2022-01-01
- 如何比较两个 NSDate:哪个是最近的? 2022-01-01
- Play 商店的设备兼容性问题 2022-01-01
- navigationItem.backBarButtonItem 不工作?为什么上一个菜单仍然显示为按钮? 2022-01-01
- 为什么姜饼模拟器方向卡在应用程序中? 2022-01-01
- Xcode UIView.init(frame:) 只能在主线程中使用 2022-01-01
- Android Volley - 如何动画图像加载? 2022-01-01
