使用 UIButtons 创建/删除/保存 NSMutableDictionary 到 NSUserDefaults
这是非常基本的问题,希望能得到回应。我找不到可以模仿的例子。我本质上希望有一个 NSMutableDictionary 在调用视图时被清除/删除。让一个按钮添加一个整数,一个单独的按钮删除该整数。最后一个按钮可将字典保存到 NSUserDefaults 并返回到上一个视图。我是否需要在每个 IBAction 或 viewDidLoad 中调用字典以首先创建它然后引用它?请指教。
示例.h
@interface example : UIViewController {
NSMutableDictionary *exampleDict;
UIButton *B1;
UIButton *B2;
UIButton *Bdone
}
-(IBAction)button1;
-(IBAction)button2;
-(IBAction)done;
@property (retain,nonatomic) IBOutlet UIButton *B1;
@property (retain,nonatomic) IBOutlet UIButton *B2;
@property (retain,nonatomic) IBOutlet UIButton *Bdone;
@property (retain,nonatomic) NSMutableDictionary *exampleDict;
@end
示例.m
@implementation example
@synthesize exampleDict;
@synthesize B1;
@synthesize B2;
@synthesize Bdone;
@end
-(IBAction)button1{
[exampleDict setValue:[NSNumber numberWithInt:1] forKey:@"one"];
}
-(IBAction)button2 {
[exampleDict removeObjectforKey: @"one"];
}
-(IBAction)done {
[[NSUserDefaults standardUserDefaults] setObject:exampleDict forKey:@"dictionaryKey"];
[self.parentViewController dismissModalViewControllerAnimated:YES];
}
-(void)viewDidLoad {
}
- (void)dealloc{
[B1 release];
[B2 release];
[Bdone release];
}
This is so basic that hopefully it will get a response. I could not find an example to model after. I essentially want to have a NSMutableDictionary that is cleared/deleted when the view is called. Have a button add an integer and a separate button remove the integer. There is a final button to save the dictionary to NSUserDefaults and return to the previous view. Do I need to call on the dictionary in each IBAction or in the viewDidLoad to first create it and then reference it? Please advise.
example.h
@interface example : UIViewController {
NSMutableDictionary *exampleDict;
UIButton *B1;
UIButton *B2;
UIButton *Bdone
}
-(IBAction)button1;
-(IBAction)button2;
-(IBAction)done;
@property (retain,nonatomic) IBOutlet UIButton *B1;
@property (retain,nonatomic) IBOutlet UIButton *B2;
@property (retain,nonatomic) IBOutlet UIButton *Bdone;
@property (retain,nonatomic) NSMutableDictionary *exampleDict;
@end
example.m
@implementation example
@synthesize exampleDict;
@synthesize B1;
@synthesize B2;
@synthesize Bdone;
@end
-(IBAction)button1{
[exampleDict setValue:[NSNumber numberWithInt:1] forKey:@"one"];
}
-(IBAction)button2 {
[exampleDict removeObjectforKey: @"one"];
}
-(IBAction)done {
[[NSUserDefaults standardUserDefaults] setObject:exampleDict forKey:@"dictionaryKey"];
[self.parentViewController dismissModalViewControllerAnimated:YES];
}
-(void)viewDidLoad {
}
- (void)dealloc{
[B1 release];
[B2 release];
[Bdone release];
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我没有看到数组的任何初始化。您应该先初始化它,然后才能向它发送消息。您还必须检查该值是否存在于用户默认值中。如果存在,您应该使用它,否则创建它。
除此之外,您可能需要在用户默认值上调用
synchronize
并在dealloc
方法中释放exampleDict
。I don't see any initialization of the array. You should initialize it before you can message to it. You will also have to check if the value exists in the user defaults. If it exists, you should use it otherwise create it.
In addition to this, you might want to call
synchronize
on the user defaults and releaseexampleDict
in thedealloc
method.