如何保存 UIButton 的属性并使用按钮加载?

How would I save a UIButton#39;s properties and load with a button?(如何保存 UIButton 的属性并使用按钮加载?)
本文介绍了如何保存 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 的属性并使用按钮加载?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Why local notification is not firing for UNCalendarNotificationTrigger(为什么没有为UNCalendarNotificationTrigger触发本地通知)
iOS VoiceOver functionality changes with Bundle Identifier(IOS画外音功能随捆绑包标识符而变化)
tabbar middle tab out of tabbar corner(选项卡栏中间的选项卡角外)
Pushing UIViewController above UITabBar(将UIView控制器推送到UITabBar上方)
Dropbox Files.download does not start when number of files in folder is gt; 1000(当文件夹中的文件数为1000时,Dropbox Files.Download不会启动)
How can I sync two flatList scroll position in react native(如何在本机Reaction中同步两个平面列表滚动位置)