hi, i have the 4.2 sdk. i am running in the simulator, not on the device. i am trying to make an app that, when the view loads, it loads data from a file, and when the app terminates, it writes the changed information (from a UIDatePicker and UITextiField) to the same file, to be loaded in again when the app starts. i want to have this application will terminate method in my view controller file because i am working with local variables and methods and it is easier.
the problem is, it doesnt even make the file. i have debugged, and it makes the file path correctly and everything, but when it checks to see if there is a file, it is always false because there isnt one. i have confirmed this by looking at my app's documents folder in finder. i am almost sure that my problem is that is not running the application will terminate method, and i looked up how to do this, even followed tutorials using nsnotificationcenter, but it still does not work, my app's documents folder is empty and it (of course) does not load data on start. i have also looked at this thread but have had no luck.
again, if you dont feel like reading all that, my problem is that my applicationWillTerminate method is not running because of something i did with NSNotificationCenter in my viewDidLoad method. thanks!!
.h
.m
code
the problem is, it doesnt even make the file. i have debugged, and it makes the file path correctly and everything, but when it checks to see if there is a file, it is always false because there isnt one. i have confirmed this by looking at my app's documents folder in finder. i am almost sure that my problem is that is not running the application will terminate method, and i looked up how to do this, even followed tutorials using nsnotificationcenter, but it still does not work, my app's documents folder is empty and it (of course) does not load data on start. i have also looked at this thread but have had no luck.
again, if you dont feel like reading all that, my problem is that my applicationWillTerminate method is not running because of something i did with NSNotificationCenter in my viewDidLoad method. thanks!!
.h
Code:
#import <UIKit/UIKit.h>
@interface saveAndLoadViewController : UIViewController {
UIDatePicker *myDate;
UITextField *myText;
}
@property (nonatomic, retain) IBOutlet UIDatePicker *myDate;
@property (nonatomic, retain) IBOutlet UITextField *myText;
@end
.m
Code:
#import "saveAndLoadViewController.h"
@implementation saveAndLoadViewController
@synthesize myDate, myText;
- (NSString *) saveFilePath {
NSArray *pathArray =
NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
return [[pathArray objectAtIndex:0] stringByAppendingPathComponent:@"savedata.plist"];
}
- (void)applicationWillTerminate:(UIApplication *)application { //from saveAndLoadAppDelegate.m file
NSArray *values = [[NSArray alloc] initWithObjects:myText.text,myDate.date,nil];
[values writeToFile:[self saveFilePath] atomically:YES];
[values release];
}
- (void)viewDidLoad {
NSString *myPath = [self saveFilePath];
BOOL fileExists = [[NSFileManager defaultManager] fileExistsAtPath:myPath];
if (fileExists) {
NSArray *values = [[NSArray alloc] initWithContentsOfFile:myPath];
myText.text = [values objectAtIndex:0];
myDate.date = [values objectAtIndex:1];
[values release];
}
UIApplication *myApp = [UIApplication sharedApplication];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(applicationWillTerminate:)
name:UIApplicationWillTerminateNotification
object:myApp];
[super viewDidLoad];
}
- (void)dealloc {
[super dealloc];
}
@end
code
Last edited: