开发者

Retrieve saved (NSUserDefaults) date to another class

开发者 https://www.devze.com 2023-03-17 10:31 出处:网络
[[NSUserDefaults standardUserDefaults] setObject:datePicker.date forKey:@\"birthDate\"]; [[NSUserDefaults standardUserDefaults] setInteger:sexSegmented.selectedSegmentIndex forKey:@\"sex\"];
[[NSUserDefaults standardUserDefaults] setObject:datePicker.date forKey:@"birthDate"];
[[NSUserDefaults standardUserDefaults] setInteger:sexSegmented.selectedSegmentIndex forKey:@"sex"];

[[NSUserDefaults standardUserDefaults] synchronize]; 

This is the value that I am saving to userdefaults and from another class I want to call them.

NSDate *birthDate = [[NSUserDefaults standardUserDefaults] objectForKey:@"birthDate"];
NSInteger a=[[NSUserDefaults standardUserDefaults] integerForKey:@"sex"];

NSLog(@"%@",birthDate);
[datePicker setDate:birthDate animated:YES];
sexSegmented.selectedSegmentIndex=a;

This is the other class from where I am get开发者_运维问答ting the saved values, but I can't see any value in birthDate. I want to take it and set the new datePicker to birthDate. Can anyone help me?


It's most likely a case of the interface outlet of datePicker not being connected properly. This would make datePicker nil and as a result you would end up storing nil in the user defaults.

You can validate this by doing

NSLog(@"%@", datePicker);

before your first snippet of code.

0

精彩评论

暂无评论...
验证码 换一张
取 消