我的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];
});
}}
viewDidLoad中
- (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];
}
答案 0 :(得分:1)
首先,您应该设置首次关闭默认值。
-(void)setDefaults
{
NSDictionary *dictionary = @{ @"BarButtonColor" : @1 }; // where 1 is white
[[NSUserDefaults standardUserDefaults] registerDefaults:dictionary];
}
现在在视图控制器的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];
});
}}
如果应用程序意外终止,您可能需要进行错误检查。