如何保存 UIButton 的属性并使用按钮加载?
我想保存 UIButton 的状态(例如它的 alpha 值以及它是否隐藏),然后当用户使用时会加载它退出并且重新加载应用程序。
我已经尝试使用 NSUserDefaults 编写一些代码,但没有成功。
有人可以帮助提供一些示例代码,以便我可以保存和加载按钮的状态吗?
谢谢,
詹姆斯
Possible Duplicate:
How can I save and load the alpha values of a UIButton in an app?
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.
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?
Thanks,
James
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
与 Shaharyar 的回答相关(我不知道如何评论):
在这种情况下,您需要使用 NSNumber。
[[NSUserDefaults standardUserDefaults] setValue:[NSNumber numberWithFloat:SOME_FLOAT] forKey:KEY];
因为
float
不是一个对象,但NSNumber
是一个对象。编辑:
1)为了确保应用程序首次运行后创建默认值:
在 AppDelegate 的
initialize
方法中:2) 更新默认值:
3) 读取默认值:
Related to Shaharyar's answer (i don't know how to comment):
in this case you need to use NSNumber.
[[NSUserDefaults standardUserDefaults] setValue:[NSNumber numberWithFloat:SOME_FLOAT] forKey:KEY];
because
float
is not an object, butNSNumber
is one.EDITED:
1) To make sure your defaults are created after the application runs at first time:
in your AppDelegate's
initialize
-method:2) Updating defaults after:
3) Read defaults:
你能发布一些代码吗?
NSUserDefaults
是存储此类信息的地方。假设:
您在设置值后是否调用了
[NSUserDefaults Synchronize]
?代码:
在你的情况下,它将是:
Can you post some of the code?
NSUserDefaults
is the place to store such information..Assumption:
Did you make a call to
[NSUserDefaults synchronize]
after setting the values?Code:
In your case it would be: