tags:

views:

128

answers:

1

I have an application that displays some graphics based on the properties of an object. I want that the state of that object to persist when the application is started next. I know that the app delegate has a "applicationwillterminate" function which I can use, but the problem is I have no idea how to get a hold of a pointer to the object.

I am trying to make this iphone app follow MVC principles, and I have the proper view and controllers. How does app delegates fit into the picture, and how can I use them? I read everywhere of advice saying to save application state during "applicationWillTerminate" but no concrete examples!

Can anyone point me to some literature or give me a hand?

Thanks!

+1  A: 

See my answer to this question. If you need more help just let me know.

David Kanarek
hey david, thanks for your reply. I do have a problem, that I find that my app delegate has no references to my object, and i have no way of saying something like appDelegate.myObject in my delegate code. The objects are declared in the controller file. Should the app delegate 'include' the controller file?
Ying
On another note, I have also tried to make the controller the delegate for UIApplicationDelegate, as in "@interface Controller : NSObject <UIApplicationDelegate>". I assumed that if I implemented applicationWillTerminate in this controller it will fire, but it didnt :(
Ying
Your AppDelegate should have an ivar that is your UIViewController. If you've set up properties for the objects you need access to, you should be able to reference them with something like `viewController.myObject` in the delegate. To create a property, in your .h, below the interface you would want something like `@property (nonatomic, retain) MyObjectType *myObject;` and in the .m inside the implementation: `@synthesize myObject;` When you have that you should have no problem.
David Kanarek
so to be sure of what you are saying, the way the delegate would get access to the controller is by doing "[[UIApplication sharedApplication] delegate] somewhere in my controller code, and then setting the myObject property in the delegate to controller.myOjbect? so like: I think this is the way to do it but want to make sure im not making any false assumptions or bad design.
Ying
Not quite, set the myObject that your UIViewController owns. If you need to access it in the delegate for saving purposes, then you use viewController.myObject
David Kanarek
so in my delegate i now have a Controller *controller; @property(nonatomic, retain) IBOutlet Controller *controller;but, the thing is this controller is always undefined(niL). I tried to set the contoller outlet in IB to point to the Controller, but i dont think that quite does it. Am i right when your last comment means that the app delegate owns a reference to the controller, but not the other way around?(except via UIApplication sharedApplication)
Ying
Yes, you are correct that the delegate owns a reference to the controller. I'm not sure how you set your application up, but if you created a standard view project, you should have a main window xib that contains a view controller, and in the app delegate, you should have a method `applicationDidFinishLaunching:` with something like `[window addSubview:viewController.view]; [window makeKeyAndVisible];` already in it. You should be able to reference the viewController at that point.
David Kanarek
Hm, i have [window makekeyvisible] but i dont have the "window addsubview.viewcontroller...." code. I think I used a window based application as my template as opposed to view template. I tried replicating the code i see in the view template but for some reason the controller just refuses to be seen. If you have any clue why this seems to be the case, do tell. BTW thanks for all the help so far its been great!
Ying
I'm not sure how to go about changing it, but here's another idea.When you create the view controller, in your `viewDidLoad` grab a reference to the delegate and set its UIViewController ivar from there: `myDelegate.viewController = self` then you can use that to save your data. You can also do all the saving and loading in the view controller, using viewDidLoad and viewDidUnload(?).
David Kanarek