【发布时间】:2014-09-25 16:15:50
【问题描述】:
我的 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];
}
【问题讨论】:
标签: ios save boolean nsuserdefaults tintcolor