user2685008
user2685008

Reputation:

passing data to app delegate

I have a class TouchDrawView which contains NSMutableArray *completeLines;. Now, when the application terminates, I want to save this array to file (I know how to do the saving, I have written it as a saveArray method of class TouchDrawView).

My problem is, in the appDelegate, I want to override say applicationDidEnterBackground to call the saveArray method on the object of my TouchDrawView class to save the array to files as I mentioned.

You see my problem? How do I pass the instance of TouchDrawView to the app delegate, so that the latter can call saveArray method on it? e.g.,

- (void)applicationDidEnterBackground:(UIApplication *)application
{

    [touchDrawViewObject saveArray]; // how to get this object?

}

ps. I am trying to avoid singleton/static data so far, just curious how it could be done otherwise.

Upvotes: 1

Views: 112

Answers (1)

Bob Vork
Bob Vork

Reputation: 2947

You could do it without a connection to your appDelegate by letting TouchDrawView observe the UIApplicationDidEnterBackgroundNotification notification. That fires when the app enters the background.

You might not want to have different save methods all over you app, but that depends the way your app is built.

Example:

[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(saveMethod:)
                                             name:UIApplicationDidEnterBackgroundNotification
                                           object:nil];

Upvotes: 1

Related Questions