如何在 NSUserdefaults 中存储 UISegmentedControle 状态
问题是当取消选择 de selectedSegmentIndex 时:“UISegmentedControlNoSegment”别名“-1”。
其他状态(0、1、2等),我可以存储为整数并使用
carTypeSegmentedControl.selectedSegmentIndex = [defaults integerForKey:@"typeOfCar"];
但-1不是NSInteger进行检索。
我还尝试从 NSUserdefaults 中删除 Integer,但请求将返回“0”,这是不可接受的。
那么,还有其他简单的方法吗?
The problem is when de selectedSegmentIndex is unselected: "UISegmentedControlNoSegment" alias "-1".
The other states (0, 1, 2 , etc.), I can store as Integers and retrieve with
carTypeSegmentedControl.selectedSegmentIndex = [defaults integerForKey:@"typeOfCar"];
But -1 is no NSInteger.
I also tried to remove the Integer out of the NSUserdefaults but a request would return an "0", which is not acceptable.
So, is there another easy way?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
-1是一个整数,它是一个负整数。您应该能够将其存储在 NSUserDefaults 中。
-1 is an integer, it's a negative integer. You should be able to store this in NSUserDefaults.
您需要使用
NSNumber
,它是常见数字和布尔类型值的对象表示,例如设置:检索:
You need to use
NSNumber
, which is an object representation of values of common numerical and boolean types, e.g. to set:To retrieve:
Ben 你是对的,-1 确实是一个整数。毕竟它被存储了。我对此做了一个 NSLog。愚蠢的我没有尝试过。我没有找到这方面的明确文档。
所以我的问题可能在其他地方。
我想你的答案也有效,亚历克斯。
谢谢你的帮助
You are right Ben, -1 is indead an integer. And it is stored after all. I did an NSLog on that. Stupid i didn't try that. I didn't find clear documentation on that.
So probably my problem is somewhere els.
I guess your answer works as well, Alex.
Thank you for helping