【问题标题】:using NSUserDefaults for TintColor change使用 NSUserDefaults 更改 TintColor
【发布时间】: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


    【解决方案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];
    
            });
        }}
    

    如果应用程序意外终止,您可能需要进行错误检查。

    【讨论】:

    • 很好,谢谢。难道你不知道如何只在一个详细视图中设置动作,而不是在所有视图中?
    • 你的意思是不注册用户默认值?如果您调用未设置的整数,它将返回 0,因此您可以在 viewDidLoad 中设置它
    猜你喜欢
    • 2015-11-02
    • 1970-01-01
    • 1970-01-01
    • 2021-02-12
    • 2015-04-10
    • 2021-12-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多