Reputation: 8942
I have a problem setting the background color of an UIView
. I'm using the following code:
self.view.backgroundColor = [UIColor colorWithRed:0
green:0.675
blue:0.929
alpha:1];
This is working fine when I'm setting the background color right at the start of the app, but when I want to update my background color after I launched my app from a notification center widget, it doesn't change the color.
EDIT – Some more details:
When the app is launched from the notification center, an AppDelegate method is called, which saves the called URL Scheme as a `NSString and calls another method in which the background color should be updated.
I've verified the method call works fine.
My problem is that the color isn't updated although the method is called.
Upvotes: 3
Views: 1333
Reputation: 8942
I have fixed my problem. In AppDelegate.m, in the method where I got the URL with which the App was launched, I initialized a new ViewController instead of using the old one, which caused all my errors. Thank you for you help!
Upvotes: 3
Reputation: 80265
Update your color in viewWillAppear
of the controller where your view appears. Set a breakpoint to make sure the code is called and the referenced view is not nil
.
If you just want to "save" the color without displaying it, use NSUserDefaults
. You can only store primitives, so you could for example do this.
// save
[[NSUserDefaults standardUserDefaults] setObject:@"0,0.675,0.929" forKey@"savedColor"];
// retrieve
NSString *colorString = [[NSUserDefaults standardUserDefaults] objectForKey:@"savedColor"];
NSArray *c = [colorString componentsSeparatedByString:@","];
CGFloat r = [c[0] floatValue];
CGFloat g = [c[1] floatValue];
CGFloat b = [c[2] floatValue];
UIColor *color = [UIColor colorWithRed:r green:g blue:b alpha:1];
Or maybe better more explicit and more concise
// save
[[NSUserDefaults standardUserDefaults] setObject:
@{@"red" : @0, @"green" : @0.675, @"blue" : @0.929} forKey:@"savedColor"];
// retrieve
NSDictionary *c = [[NSUserDefaults standardUserDefaults] objectForKey@"savedColor"];
UIColor *color = [UIColor
colorWithRed:c[@"red"] green:c[@"green"] blue:c[@"blue"] alpha:1];
Now you put it all into convenience methods:
-(void)saveColorToUserDefaults:(UIColor*)color withKey:(NSString*)key {
CGFloat red, green, blue, alpha;
[color getRed:&red green:&green blue:&blue alpha:&alpha];
[[NSUserDefaults standardUserDefaults] setObject:
@{@"red": @(red), @"green" : @(green), @"blue" : @(blue), @"alpha" : @(alpha)}
forKey: key];
}
-(UIColor*)colorFromUserDefaultsWithKey:(NSString *)key {
NSDictionary *c = [[NSUserDefaults standardUserDefaults] objectForKey@"savedColor"];
return = [UIColor colorWithRed:c[@"red"] green:c[@"green"]
blue:c[@"blue"] alpha:c[@"alpha"]];
}
Upvotes: 0
Reputation: 31
if you are launching the app from notification center, means that the app is coming from background to foreground. In that case you need to handle the uiview background in methods applicationDidEnter background & applicationDidEnterForeground
Upvotes: 0