gpt4 book ai didi

ios - 使用 NSUserDefaults 改变 TintColor

转载 作者:行者123 更新时间:2023-11-29 02:46:13 25 4
gpt4 key购买 nike

我的 UINavigationBar 中有一个 UIBarButtonItem。有一张星星的照片(白色的)。如果用户点击星星,我已经将星星的色调设置为黄色,作为最喜欢的意思。如果用户点击一次白色星形,它会改变颜色,然后用户关闭应用程序。如果他确实点击它,我仍然希望选择星星。它有效,但再次运行后不要保存。有什么想法吗?

static UIBarButtonItem *barb1;
@interface ViewControllerDetail (){
BOOL kokos;
}

-(void)changestarr{
if (kokos) {
[barb1 setTintColor:[UIColor whiteColor]];
kokos = NO;

} else {
[barb1 setTintColor:[UIColor yellowColor]];
kokos = YES;
prefs = [NSUserDefaults standardUserDefaults];
[prefs setObject:[UIColor yellowColor] forKey:@"yess"];
[prefs synchronize];
NSString *message = @"You got it.";

UIAlertView *toast = [[UIAlertView alloc] initWithTitle:nil
message:message
delegate:nil
cancelButtonTitle:nil
otherButtonTitles:nil, nil];
[toast show];

int duration = 1.7;

dispatch_after(dispatch_time(DISPATCH_TIME_NOW, duration * NSEC_PER_SEC), dispatch_get_main_queue(), ^{
[toast dismissWithClickedButtonIndex:0 animated:YES];

});
}}

查看加载

- (void)viewDidLoad
{
[super viewDidLoad];
kokos = NO;
barb1 =[[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"star.png"]
style:UIBarButtonItemStyleDone
target:self
action:@selector(changestarr)];

barb2=[[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"final.png"]
style:UIBarButtonItemStyleDone
target:self
action:@selector(showfinal)];
[barb1 setTintColor:[UIColor whiteColor]];

self.navigationItem.rightBarButtonItems = [[NSArray alloc] initWithObjects:barb1, barb2, nil];

}

最佳答案

你好,首先你应该设置你的第一个默认值。

-(void)setDefaults
{
NSDictionary *dictionary = @{ @"BarButtonColor" : @1 }; // where 1 is white

[[NSUserDefaults standardUserDefaults] registerDefaults:dictionary];

}

现在在 View Controller 的 init 方法中调用 -(void)setDefaults。然后将 -(void)viewDidLoad 更改为以下内容。

- (void)viewDidLoad
{
[super viewDidLoad];

// If you do not want to set defaults in init

if ([[NSUserDefaults standardUserDefaults] integerForKey: @"BarButtonColor"] == 0) {
[[NSUserDefaults standardUserDefaults] setInteger:1 forKey: @"BarButtonColor"];
}



kokos = NO; // Remove
barb1 =[[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"star.png"]
style:UIBarButtonItemStyleDone
target:self
action:@selector(changestarr)];

barb2=[[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"final.png"]
style:UIBarButtonItemStyleDone
target:self
action:@selector(showfinal)];
// changes
NSInteger color = [[NSUserDefaults standardUserDefaults] integerForKey: @"BarButtonColor"];

if (color == 1) {
[barb1 setTintColor:[UIColor whiteColor]];
} else if (colour == 2) {
[barb1 setTintColor:[UIColor yellowColor]];
}

// finished changes

self.navigationItem.rightBarButtonItems = [[NSArray alloc] initWithObjects:barb1, barb2, nil];

}

然后最后把-(void)changstarr改成下面的

-(void)changestarr{

NSInteger color = [[NSUserDefaults standardUserDefaults] integerForKey: @"BarButtonColor"];

if (color == 2) {
[barb1 setTintColor:[UIColor whiteColor]];
[[NSUserDefaults standardUserDefaults] setInteger:1 forKey:@"BarButtonColor"]];

} else if (color == 1) {
[barb1 setTintColor:[UIColor yellowColor]];
[[NSUserDefaults standardUserDefaults] setInteger:2 forKey:@"BarButtonColor"]];
NSString *message = @"You got it.";

UIAlertView *toast = [[UIAlertView alloc] initWithTitle:nil
message:message
delegate:nil
cancelButtonTitle:nil
otherButtonTitles:nil, nil];
[toast show];

int duration = 1.7;

dispatch_after(dispatch_time(DISPATCH_TIME_NOW, duration * NSEC_PER_SEC), dispatch_get_main_queue(), ^{
[toast dismissWithClickedButtonIndex:0 animated:YES];

});
}}

您可能希望对应用程序是否意外终止进行错误检查。

关于ios - 使用 NSUserDefaults 改变 TintColor,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25097518/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com