Reputation: 14205
I've a class named ContentController. I create its instance from ViewController. The first fellow fetches some data from my remote server, does something on it. It then pass the information to ViewController which shows something good to the user. So far so good.
Now, the problem is while using the AppDelegate. When the application tries to enter in the background mode, I want to access the same instance (of ContentController). And save few attributes on the device. And this is not working.
Would you please help me ?
Upvotes: 1
Views: 1649
Reputation: 1715
If you really want to access the ContentController instance from your AppDelegate you could create a property in the AppDelegate.
//AppDelegate.h
@property (strong, nonatomic) ContentController *contentController;
When you need to use it in your ViewController you could use,
AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
appDelegate.contentController = [[ContentController alloc] init];
Or you could create a property in our ViewController class which points to the AppDelegate Instance.
self.contentController = appDelegate.contentController;
Upvotes: 2
Reputation: 2052
To save property values on applicationDidEnterBackground: you can add a notification observer in your class'es viewDidLoad method like this:
UIApplication *app = [UIApplication sharedApplication];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(applicationWillResignActive:) name:UIApplicationWillResignActiveNotification object:app];
In viewDidUnload you should add this:
[[NSNotificationCenter defaultCenter] removeObserver:self];
Also add these two methods in your class .m-file (below you can see example from one of my applications):
- (void)applicationWillResignActive:(NSNotification *)notification {
NSMutableArray *array = [[NSMutableArray alloc] init];
[array addObject:[NSNumber numberWithInt:self.event.eventID]];
[array addObject:self.event.eventName];
[array addObject:[NSNumber numberWithDouble:[self.event.ticketPrice doubleValue]]];
[array writeToFile:[self dataFilePath] atomically:YES];
[array release];
}
And this method will load data from your file, call in in your viewDidLoad:
- (void)loadEventFromEventPlist {
NSString *filePath = [self dataFilePath];
if ([[NSFileManager defaultManager] fileExistsAtPath:filePath]) {
NSArray *array = [[NSArray alloc] initWithContentsOfFile:filePath];
self.event.eventID = [[array objectAtIndex:0] intValue];
self.event.eventName = [array objectAtIndex:1];
self.event.ticketPrice = [NSNumber numberWithDouble:[[array objectAtIndex:2] doubleValue]];
[array release];
}
}
This method is needed to get file name:
- (NSString *)dataFilePath {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
return [documentsDirectory stringByAppendingPathComponent:@"data.plist"];
}
Upvotes: 0
Reputation: 5181
Register a notification from your ContentController.
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(applicationWillResignActive:) name:UIApplicationWillResignActiveNotification object:nil];
And Implement a applicationWillResignActive:
method inside Content controller to do what ever you want.
- (void)applicationWillResignActive:(NSNotification *)notification
{
// Your server calls
}
Upvotes: 1