[英]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
. 但是如果UIControlStateSelected
为YES
,它将为按钮设置不需要的覆盖背景。 : :
- (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.