开发者

What message is sent to a UIView when the application terminates?

开发者 https://www.devze.com 2023-01-06 12:26 出处:网络
I am trying to locate a message which I can override and then save changes to my a开发者_JS百科pplication.

I am trying to locate a message which I can override and then save changes to my a开发者_JS百科pplication.

On MainWindow.xib I have placed a UIView and set its class (in interface builder) to be my Custom view TouchDrawView.

In TouchDrawView I have a bunch of code for handling touch events and 2 arrays which track these touch events.

My application is launched by the AppDelegate but it has no reference to this TouchDrawView. It simply launches the application.

What I want to do is save my 2 arrays when the application terminates - I can do this in the TouchDrawView but I don't know what message this UIView gets sent when the whole application is about to terminate and I can't do it in the AppDelegate because it doesn't have a reference to the 2 array or the custom UIView


UIView instances will not get send any messages when the app will terminate.

There's another easy way to get notified of app state changes: notifications. You can register for notifications sent through [NSNotificationCenter defaultCenter].

For older versions of iPhone OS there's a UIApplicationWillTerminateNotification. Beginning from iOS 4 you should also listen for UIApplicationDidEnterBackgroundNotification, to prepare for termination.


The short, unhelpful answer is that a UIView is not sent a message when the application terminates.

You need to think in terms of Model-View-Controller because that's how Cocoa and Cocoa Touch are designed. Of those three, if a message was directly sent to any it would be the controller. In your case that would be the UIViewController that talks to your view.

The bad news is there is no such message there either.

An application shuts down, not an individual screen/view.

There are (at least) two ways of doing what you want to do. Firstly, you could save the state of the view controller when it is released. (That kind of feels wrong to me.)

They way that I do it in my app is to listen for the applicationWillTerminate message in your UIApplicationDelegate and traverse the view hierarchy and save the state of each view controller. When the system starts up you can do the opposite. I blogged about it here.

0

精彩评论

暂无评论...
验证码 换一张
取 消