Reputation: 35
I'm developing an app for iPad (IOS7) in Xcode 5 using storyboards. I'm having a strange issue:
I call this method from a button press:
-(void)navigateToProjectsPage {
// Save the currently open subject
NSString *detailDescription;
NSString *imageDescription;
// Get the information to be saved
detailDescription = _detailDescriptionTextView.text;
imageDescription = _imageDescriptionTextView.text;
// Create the 2 save keys
NSString *detailDescriptionKey = [NSString stringWithFormat:@"%@%@", [self.detailItem description], @"-detailDescription"];
NSString *imageDescriptionKey = [NSString stringWithFormat:@"%@%@", [self.detailItem description], @"-imageDescription"];
NSUserDefaults *myDefaults = [NSUserDefaults standardUserDefaults];
[myDefaults setObject:detailDescription forKey:detailDescriptionKey];
[myDefaults setObject:imageDescription forKey:imageDescriptionKey];
[myDefaults synchronize];
// Close this project
[self performSegueWithIdentifier:@"projectsSegue" sender:nil];
// Show the selectSubjectBanner
_selectSubjectBanner.hidden = NO;
}
Which is supposed to save some newly entered data and then modal segue to a new view controller. Whenever I dismiss the new VC and go back to this one and try to load the saved item, it returnes nothing - like if it wasn't saved. However, if I just close my app in the multitasking thing at once, without pressing the button - it saves using the AppDelegate.m method:
- (void)applicationDidEnterBackground:(UIApplication *)application
{
// Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
// If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
// Save what the user entered
NSString *detailDescription;
NSString *imageDescription;
// Get the information to be saved
detailDescription = _detailViewController.detailDescriptionTextView.text;
imageDescription = _detailViewController.imageDescriptionTextView.text;
// Create the 2 save keys
NSString *detailDescriptionKey = [NSString stringWithFormat:@"%@%@", ([self.detailViewController.detailItem description]), @"-detailDescription"];
NSString *imageDescriptionKey = [NSString stringWithFormat:@"%@%@", [self.detailViewController.detailItem description], @"-imageDescription"];
// Save the data
[[NSUserDefaults standardUserDefaults] setObject:detailDescription forKey:detailDescriptionKey];
[[NSUserDefaults standardUserDefaults] setObject:imageDescription forKey:imageDescriptionKey];
}
Any clue why?
Thanks!
Upvotes: 0
Views: 118
Reputation: 35
The probem was that old information remained in the system after entering a new UIViewController when it's supposed to be cleared - wrote this code that fixed it:
-(void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
// Clear as we're about to open a new project/exit
_detailViewController.detailItem = @"";
selectedSubject = @"";
}
You may need to create a BOOL like I did (not in this code snippet) so that it won't interfere with other functions if you're using a MasterDetail Template
Upvotes: 0
Reputation: 11994
Wain's answer is correct. Description of detailItem will look like <UIClassName: 0x2ad7b00>
. Also, you asking why your data isn't saved when you navigating. That's because detailItem is released when your detail view controller is hidden. When your controller is being shown again, detailItem will be allocated again, so it will have different address. Thus, description will be different to.
However, when your application goes background detail view controller stay in memory, and address of the detailItem doesn't change.
Upvotes: 0
Reputation: 119031
[self.detailItem description]
is unlikely to do what you want. It probably includes the instance pointer of self.detailItem
, which will change when the detail item changes (and even when you reload with the same detail item) so your key won't match. If anything else changed about the detail item, that could also have an effect.
Keys in user defaults should usually have nice static keys that you can find again.
Too see what you've been adding so far, log [[NSUserDefaults standardUserDefaults] dictionaryRepresentation]
and search for your -detailDescription
tag.
Also, in your case, using user defaults for this short term style storage (that's what it looks like anyway) probably isn't best. You could be better server by creating and storing (and passing as a property if required) this data in a dictionary.
Upvotes: 1