使用 UIButtons 创建/删除/保存 NSMutableDictionary 到 NSUserDefaults

发布于 2024-11-09 10:19:41 字数 1204 浏览 0 评论 0原文

这是非常基本的问题,希望能得到回应。我找不到可以模仿的例子。我本质上希望有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

触ぅ动初心 2024-11-16 10:19:41

我没有看到数组的任何初始化。您应该先初始化它,然后才能向它发送消息。您还必须检查该值是否存在于用户默认值中。如果存在,您应该使用它,否则创建它。

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];

    exampleDict = [[[NSUserDefaults standardUserDefaults] objectForKey:@"dictionaryKey"] mutableCopy];
    if ( !exampleDict ) {
        exampleDict = [[NSMutableDictionary alloc] init];
    }
}

除此之外,您可能需要在用户默认值上调用 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.

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];

    exampleDict = [[[NSUserDefaults standardUserDefaults] objectForKey:@"dictionaryKey"] mutableCopy];
    if ( !exampleDict ) {
        exampleDict = [[NSMutableDictionary alloc] init];
    }
}

In addition to this, you might want to call synchronize on the user defaults and release exampleDict in the dealloc method.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文