I can't seem to save data to a custom instance object in my AppDelegate. My custom class is very simple and is as follows:
Person.h
...
@interface Person : NSObject {
int _age;
}
- (void) setAge: (int) age;
- (int) age;
@end
Person.m
#import "Person.h"
@implementation Person
- (void) setAge:(int) age {
_age = age;
}
- (int) age {
return _age;
}
@end
I then create an instance of Person in the AppDelegate class:
AppDelegate.h
@class Person;
@interface AccuTaxAppDelegate : NSObject <UIApplicationDelegate> {
...
Person *person;
}
...
@property (nonatomic, retain) Person *person;
@end
AppDelegate.m
...
#import "Person.h"
@implementation AccuTaxAppDelegate
...
@synthesize person;
- (void)applicationDidFinishLaunching:(UIApplication *)application {
// Override point for customization after app launch
[window addSubview:[navigationController view]];
[window makeKeyAndVisible];
}
- (void)applicationWillTerminate:(UIApplication *)application {
// Save data if appropriate
}
#pragma mark -
#pragma mark Memory management
- (void)dealloc {
[navigationController release];
[window release];
[person release];
[super dealloc];
}
@end
Finally, in my ViewController code I grab a handle on AppDelegate and then grab the person instance, but when I try to save the age it doesn't seem to work:
MyViewController
...
- (void)textFieldDidEndEditing:(UITextField *)textField
{
NSString *textAge = [textField text];
int age = [textAge intValue];
NSLog(@"Age from text field::%i", age);
AppDelegate *appDelegate =
(AppDelegate *)[UIApplication sharedApplication].delegate;
Person *myPerson = (Person *)[appDelegate person];
NSLog(@"Age before setting: %i", [myPerson age]);
[myPerson setAge:age];
NSLog(@"Age after setting: %i", [myPerson age]);
[textAge release];
}
...
The output of the above NSLogs are:
[Session started at 2010-05-04 18:29:22 +0100.]
2010-05-04 18:29:28.260 AccuTax[16235:207] Age in text field:25
2010-05-04 18:29:28.262 AccuTax[16235:207] Age before setting: 0
2010-05-04 18:29:28.263 AccuTax[16235:207] Age after setting: 0
Any ideas why 'age' isn't being stored? I'm relatively new to Obj-C so please forgive me if I'm missing something very simple!