Reputation: 733
I have a main view controller, a "change background color" view controller, and a detail view controller.
When a user changes the background color via the change background color view controller, I do the following:
NSData *colorData = [NSKeyedArchiver archivedDataWithRootObject:currentColor];
[[NSUserDefaults standardUserDefaults] setObject:colorData forKey:@"backgroundColor"];
//currentColor is specified by user
This gets updated to the other view controllers through:
NSData *colorData = [[NSUserDefaults standardUserDefaults] objectForKey:@"backgroundColor"];
UIColor *color = [NSKeyedUnarchiver unarchiveObjectWithData:colorData];
self.view.backgroundColor = color;
The problem I have is that the main view controller loads FIRST. Currently everything has a white screen when the app loads for the first time. My question is: how would I change it so that the application loads with a green color on every view controller to begin with, and after the user specifies the color, it stops displaying the green color and starts displaying the specified color?
I tried this with viewDidLoad
but that doesn't work because it will just override the NSUserDefaults
setting.
Upvotes: 0
Views: 346
Reputation: 968
As for your intention with setting your background to green every time until the user selects a color in your color selection view, would you like for this be done everytime the app is loaded into the foreground or everytime the app is launched? I suppose you could just set it up in your IB files (if you're using IB) or viewDidLoad
should work.
If I'm understanding what you're trying to do correctly you could create another item in NSUserDefaults
to track when the user has made a color selection, then you just check this flag when you're in viewDidLoad
before setting the background to the selected color. However the reason I asked when you wanted to do this is to know when to reset that flag.
Upvotes: 1
Reputation: 29886
-[NSUserDefaults registerDefaults:]
is the thing you want here. You call that with the values you want to be the "defaults" and then other callers will get those values. Reference here. You'll want this to run early in your app's lifecycle, probably in – application:didFinishLaunchingWithOptions:
Upvotes: 3