简体   繁体   English

使用NSUSerDefaults如何在两次按下按钮时更改颜色

[英]How when Button pressed twice the color should change using NSUSerDefaults

I am new to XCode. 我是XCode的新手。 I used with NSUserDefaults even though it is not coming what i required means If i click one time on that button it should change the colour to green and if i press it again it should change the colour to black. 我使用了NSUserDefaults即使它没有达到我所需要的含义,如果我在该按钮上单击一次,它应该将颜色更改为绿色 ,如果再次按下它,则应该将颜色更改为黑色。

Here my Code is - 这是我的代码-

- (IBAction)subscribeButtonAction:(id)sender {
    if (count == 0) {
        [_subscribeButtonObj setBackgroundColor:[UIColor greenColor]];
        greenStr=[NSString stringWithFormat:@"green"];
        NSUserDefaults *greendefults=[NSUserDefaults standardUserDefaults];
        [greendefults setValue:greenStr forKey:@"greencolor"];
        [greendefults synchronize];
        ///
        count++;
    } else if(count == 1){
        [_subscribeButtonObj setBackgroundColor:[UIColor blueColor]];
        blackStr=[NSString stringWithFormat:@"black"];
        NSUserDefaults *blackDefaults=[NSUserDefaults standardUserDefaults];
        [blackDefaults setValue:blackStr forKey:@"blackcolor"];
        [blackDefaults synchronize];
        //count = 0;
    }
}

In ViewWillAppear I wrote the code like this - ViewWillAppear我编写了这样的代码-

-(void)viewWillAppear:(BOOL)animated
{
    //count = 0;
    if ([[NSUserDefaults standardUserDefaults] stringForKey:@"greencolor"]) {
        NSLog(@"change the button to green color %@",[[NSUserDefaults standardUserDefaults] stringForKey:@"greencolor"]);
    } else {
        NSLog(@"change the button to blackcolor ");
    }
}

Can anyone please help me. 谁能帮帮我吗。 Thanks in Advance 提前致谢

There's no need of NSUserDefaults to implement this. 无需NSUserDefaults即可实现。 Its simple to handle with UIButton 's control states, 使用UIButton的控件状态很容易处理,

  • UIControlStateNormal
  • UIControlStateSelected

For example, 例如,

- (IBAction)changeColor:(id)sender {
    UIButton *button = (UIButton *)sender;
    if (button.selected) { // If selected will change the color into Red
        button.backgroundColor = [UIColor redColor];
        [button setSelected:NO];
    } else {
        button.backgroundColor = [UIColor greenColor];
        [button setSelected:YES];
    }
}

And, keeep in mind you can change your button's state initially like setting this, 而且,请记住,您可以像设置此设置一样最初更改按钮的状态,

[myButton setSelected:YES];

Assign, this code in your viewDidLoad 在您的viewDidLoad分配此代码

Cheers!! 干杯!!

If you wanna do it the NSUserDefault way. 如果您想使用NSUserDefault方式。 Setting control states is a nice way though. 设置控制状态是一种不错的方法。 but It will set a unwanted overlay background to the button if UIControlStateSelected is YES . 但是如果UIControlStateSelectedYES ,它将为按钮设置不需要的覆盖背景。 :

- (IBAction)btnAction:(id)sender {
    NSUserDefaults *color=[NSUserDefaults standardUserDefaults];
    if (count == 0) {
        [self.btnOutlet setBackgroundColor:[UIColor greenColor]];
        [color setValue:@"green" forKey:@"color"];

    }else if(count == 1){
        [self.btnOutlet setBackgroundColor:[UIColor blueColor]];
        [color setValue:@"blue" forKey:@"color"];

    }
    count = !count;
}


- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    count = 0;

    if([[[NSUserDefaults standardUserDefaults] valueForKey:@"color"] isEqualToString:@"green"]){ // been used
        [self.btnOutlet setBackgroundColor:[UIColor greenColor]];
    }else if([[[NSUserDefaults standardUserDefaults] valueForKey:@"color"] isEqualToString:@"blue"]){
        [self.btnOutlet setBackgroundColor:[UIColor blueColor]];
    }else{ // first time
        [self.btnOutlet setBackgroundColor:[UIColor yellowColor]];
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用TouchableHighlight在React Native中按下时如何更改按钮颜色? - How to change button color when pressed in React Native using TouchableHighlight? 如何在按下按钮时更改按钮的颜色,并在按下其他按钮时重置为原始颜色? - How do I change a button's color when pressed and reset to original color when a different button is pressed? 按下下一个按钮时如何更改上一个按钮的颜色? - How to Change previous button color when pressed the next button? 当在UIActivityViewController上单击“取消”按钮时,如何更改NavigationController的文本颜色? - How to change NavigationController text color when Cancel button is pressed on UIActivityViewController? 按下时更改以编程方式创建的按钮的颜色 - Change Color of Programmatically Created Button when Pressed 如何使用Sprite Kit和Swift 2按下按钮时改变场景 - How to change scene when button is pressed using Sprite Kit and swift 2 按下时如何使 UIView 改变颜色? - How to make UIView change color when pressed? 按下按钮时更改自定义UIButton(CoreGraphics)的背景颜色 - change background color of custom UIButton (CoreGraphics) when button is pressed 将按钮按下到主机应用程序时如何更改自定义键盘的背景颜色? - How to change background color of custom keyboard when button is pressed into host app? 按下按钮时如何更改徽章编号? - How to change badge number when button is pressed?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM