Good Day.
On my AppDelegate.h
@property (nonatomic, retain) Config *AppConfig;
Within my AppDelegate.m, I can access this without problems.
UIAlertView *alert =
[[UIAlertView alloc] initWithTitle:@"Settings Saved"
message:[NSString stringWithFormat:@"%@\n%@\n%@\n%@\n%@",
AppConfig.Username, AppConfig.Password,
AppConfig.Server, AppConfig.LastRefNo,
AppConfig.LastSync]
delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alert show];
[alert release];
AppConfig is being populated within AppDelegate as follows
- (void)GetSettings
{
// Define our table/entity to use
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Config" inManagedObjectContext:managedObjectContext];
// Setup the fetch request
NSFetchRequest *request = [[NSFetchRequest alloc] init];
[request setEntity:entity];
// Define how we will sort the records
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"Username" ascending:NO];
NSArray *sortDescriptors = [NSArray arrayWithObject:sortDescriptor];
[request setSortDescriptors:sortDescriptors];
[sortDescriptor release];
// Fetch the records and handle an error
NSError *error;
NSMutableArray *mutableFetchResults = [[managedObjectContext executeFetchRequest:request error:&error] mutableCopy];
if (mutableFetchResults != nil && [mutableFetchResults count] > 0) {
// Save our fetched data
AppConfig = [mutableFetchResults objectAtIndex:0];
}
else
{
AppConfig = nil;
}
[mutableFetchResults release];
[request release];
}
If I try to access AppConfig from another ViewController through this
- (void)loadAccount
{
SpotlightAppDelegate *_appDelegate = (SpotlightAppDelegate *) [[UIApplication sharedApplication] delegate];
txtUsername.text = [_appDelegate.AppConfig valueForKey:@"Username"];
txtPassword.text = [_appDelegate.AppConfig valueForKey:@"Password"];
}
NSUnknownKeyException is thrown on line 2.
Thanks in advance.
Ok I found it after resting for sometime 🙂
I should have called retain on AppConfig.