iosobjective-ctheoslogos

Can iOS colors use custom values?


If a custom color is defined by UIColor and a variable is used in the color (e.g. colorWithAlpha:balfa) the variable will not be used and instead the alpha will default to the goto value.

Example:

NSDictionary *dict = [[NSDictionary alloc] initWithContentsOfFile:@"/var/mobile/Library/Preferences/com.app.me~prefs.plist"];

static CGFloat bart;

static void PrefLoad()
{
    bart = [dict objectForKey:@"bartf"] ? [[dict objectForKey:@"bartf"] floatValue] : 0.42;
}

UIColor *color = [UIColor colorWithRed:0 green:0 blue:0 alpha:bart];

In this case the alpha value is automatically set to 0.42 (defined as a default) even when the user has changed the value of the slider to something higher or lower.

Any ideas on how to fix this?


Solution

  • The problem is in the code that sets the bartf value in the dictionary.

    THIS:

    bart = [dict objectForKey:@"bartf"] ? [[dict objectForKey:@"bartf"] floatValue] : 0.42;

    explains why it never changes. [dict objectForKey:@"bartf"] is always null.

    Make sure your code is updating that dictionary correctly. To debug add a:

    NSLog(@"%@", dict);

    after where you create this dictionary, and make sure the value is there.