Still pretty new to ObjectiveC and Cocoa here. Hopefully the answer to this question is straight forward but not painfully obvious.
I’ve got a simple application that uses a couple text fields to setup a couple of strings that are searched for a string in a third text field and the result is displayed in a label. For this exercise I’m using the -rangeOfString method of NSString. To make things more fun I decided to add a checkbox to do case sensitive searches.
That all works great except one thing: The state of the checkbox and the BOOL it “controls” are out of sync when the application starts. In IB the checkbox state is set to on. The target of the checkbox’s action is a method -(IBAction)checkboxChange which evaluates the checkbox state and sets a BOOL accordingly. The BOOL is created with a value of NO.
Currently I’ve got a line of code in -awakeFromNib that sets the BOOL to YES. What I’m after is a way to have the checkbox send it’s action when the application is loaded so the BOOL is set consistent with the checkbox state. I thought I could use -applicationDidFinishLoading in AppDelegate but I don’t know how I’d refer to the checkbox there. I also thought I could set the value of the BOOL in that method but even though I’ve imported the .h file where the BOOL is defined XCode complains the identifier is undefined.
And should it be relevant, all my code is in a class named AppController and I’m not using the default AppDelegate at all.
Thanks for reading and for your suggestions / answers!
NSUserDefaultsis your best friend for persisting preferences state across application launches. Make sure you call thesynchronizemethod after making a change to persist it to disk. In some instances, you may want to delay synchronizing until the app is about to be put in a background state.NSUserDefaults Class Reference
Sample code:
To restore it when the app launches again: