-3

如果自定义颜色由 UIColor 定义并且在颜色中使用了变量(例如 colorWithAlpha:balfa),则不会使用该变量,而 alpha 将默认为 goto 值。

例子:

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];

在这种情况下,即使用户将滑块的值更改为更高或更低的值,alpha 值也会自动设置为 0.42(定义为默认值)。

有想法该怎么解决这个吗?

4

2 回答 2

1

将您的代码更改为

NSNumber *bartNum = dict[@"bartf"];
NSLog(@"bartNum = %@", bartNum);

static CGFloat bart;

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

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

并在调试窗口中发布你有什么。

于 2014-04-15T21:17:12.620 回答
0

问题在于在字典中设置 bartf 值的代码。

这个:

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

解释了为什么它永远不会改变。[dict objectForKey:@"bartf"]始终为 null

确保您的代码正确更新该字典。要调试添加:

NSLog(@"%@", 字典);

在您创建此字典的位置之后,并确保该值在那里。

于 2014-04-16T07:05:39.823 回答