0

私は大きな問題を抱えており、すでに数日間解決できませんでした。

アプリが初めてロードされるとき、最初の UIViewController で NSUserDeafaults に 4 つの色を保存しています。

ViewController.m

AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
NSUserDefaults *sharedDefaults = [NSUserDefaults standardUserDefaults];
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSInteger countValue = [defaults integerForKey:@"Array"];

if ([sharedDefaults boolForKey:@"FirstLaunch"])
{
    [self openSubView];
    [sharedDefaults setBool:NO forKey:@"FirstLaunch"];
    [sharedDefaults synchronize];
    [self saveColorsToDefaults];

    // [prefs synchronize];
}   // Do any additional setup after loading the view.
else if(countValue == 1)
{
}

}


-(void)saveColorsToDefaults{

const CGFloat  *components1 = CGColorGetComponents([UIColor darkGrayColor].CGColor);
const CGFloat  *components2 = CGColorGetComponents([UIColor blueColor].CGColor);
const CGFloat  *components3 = CGColorGetComponents([UIColor redColor].CGColor);
const CGFloat  *components4 = CGColorGetComponents([UIColor purpleColor].CGColor);

NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];

[prefs setFloat:components1[0]  forKey:@"cr"];
[prefs setFloat:components1[1]  forKey:@"cg"];
[prefs setFloat:components1[2]  forKey:@"cb"];
[prefs setFloat:components1[3]  forKey:@"ca"];

[prefs setFloat:components2[0]  forKey:@"cr2"];
[prefs setFloat:components2[1]  forKey:@"cg2"];
[prefs setFloat:components2[2]  forKey:@"cb2"];
[prefs setFloat:components2[3]  forKey:@"ca2"];

[prefs setFloat:components3[0]  forKey:@"cr3"];
[prefs setFloat:components3[1]  forKey:@"cg3"];
[prefs setFloat:components3[2]  forKey:@"cb3"];
[prefs setFloat:components3[3]  forKey:@"ca3"];

[prefs setFloat:components4[0]  forKey:@"cr4"];
[prefs setFloat:components4[1]  forKey:@"cg4"];
[prefs setFloat:components4[2]  forKey:@"cb4"];
[prefs setFloat:components4[3]  forKey:@"ca4"];

[prefs synchronize];
NSLog(@"I just saved colors");


}

そして、これらの色を `DrawViewControllers 描画ツールに設定する必要があります。最後の3色しか出ませんが、1色目が出ません。

しかし、経由で最初の楽器の色を設定しcolorPicker、すべての色を再度保存するとNSUserDefaults、うまく機能します!

 -(void)LoadColorsAtStart
{

NSUserDefaults *prefers = [NSUserDefaults standardUserDefaults];
UIColor* tColor = [UIColor colorWithRed:[prefers floatForKey:@"cr"] green:[prefers floatForKey:@"cg"] blue:[prefers floatForKey:@"cb"] alpha:[prefers floatForKey:@"ca"]];

UIColor* tColor2 = [UIColor colorWithRed:[prefers floatForKey:@"cr2"] green:[prefers floatForKey:@"cg2"] blue:[prefers floatForKey:@"cb2"] alpha:[prefers floatForKey:@"ca2"]];

UIColor* tColor3 = [UIColor colorWithRed:[prefers floatForKey:@"cr3"] green:[prefers floatForKey:@"cg3"] blue:[prefers floatForKey:@"cb3"] alpha:[prefers floatForKey:@"ca3"]];

UIColor* tColor4 = [UIColor colorWithRed:[prefers floatForKey:@"cr4"] green:[prefers floatForKey:@"cg4"] blue:[prefers floatForKey:@"cb4"] alpha:[prefers floatForKey:@"ca4"]];

[prefers synchronize];

[self extractRGBforBlack:tColor];
[self extractRGBforBlue:tColor2];
[self extractRGBforRed:tColor3];
[self extractRGBforLine:tColor4];

[self.colorBar1 setTextColor:self.blackExtract];
[self.colorBar2 setTextColor:self.blueExtract];
[self.colorBar3 setTextColor:self.redExtract];
[self.colorBar4 setTextColor:self.lineExtract];

NSLog(@"I have extracted colors");   
}

私は 5 つの類似したものを持っておりDrawViewControllers、すべて Drawing で動作しUIViewます。

4

2 に答える 2

0

これを試してみてください。エラーなしで動作します

1. UIColor を NSString として NSUserDefaults に保存する

-(void)storeColor
{
    NSString *colString = [NSString stringWithFormat:@"%@", [UIColor whiteColor]];
    [[NSUserDefaults standardUserDefaults] setObject:colString forKey:@"keyFontColor"];
}

2.取得中に文字列をUIColorに変換する

-(void)retriveColor
    NSArray *values = [[[NSUserDefaults standardUserDefaults] objectForKey:@"keyFontColor"] componentsSeparatedByString:@" "];
    NSLog(@"%d",[values count]);
    UIColor *def_color = [[UIColor alloc] init];
    if ([values count]==5) {
        CGFloat red = [[values objectAtIndex:1] floatValue];
        CGFloat green = [[values objectAtIndex:2] floatValue];
        CGFloat blue = [[values objectAtIndex:3] floatValue];
        CGFloat alpha = [[values objectAtIndex:4] floatValue];
        def_color = [UIColor colorWithRed:red green:green blue:blue alpha:alpha];
        self.dateLabel.textColor = def_color;

    }
    else if ([values count]==3)
    {
        CGFloat white = [[values objectAtIndex:1] floatValue];
        CGFloat alpha = [[values objectAtIndex:2] floatValue];
        def_color = [UIColor colorWithWhite:white alpha:alpha];
        self.label.textColor = def_color;        

    }
    else
    {
        // its store default value while app loads first time
        self.label.textColor.textColor = [UIColor blackColor];

    }

}
于 2013-12-10T11:42:28.300 に答える