Reputation: 345
I am new to 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.
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 -
-(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
Upvotes: 0
Views: 283
Reputation:
If you wanna do it the NSUserDefault way. Setting control states is a nice way though. but It will set a unwanted overlay background to the button if UIControlStateSelected
is 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]];
}
}
Upvotes: 0
Reputation: 24476
There's no need of NSUserDefaults
to implement this. Its simple to handle with UIButton
's control states,
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
Cheers!!
Upvotes: 1