Reputation: 345
I used with NSUserDefaults even though it is not coming what i requried 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 balck....
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 ");
}
}
Upvotes: 0
Views: 148
Reputation: 1201
in its button action
- (IBAction)didTapButton:(id)sender {
UIButton *btn = (UIButton *)sender;
if (btn.isSelected == NO)
{
btn.selected =YES;
btn.backgroundColor = [UIColor greenColor];
}
else
{
btn.selected =NO;
btn.backgroundColor = [UIColor blackColor];
}
}
Upvotes: 0
Reputation: 2439
hope this will help you
if you make it through .xib file then make an action event like
- (IBAction)changeColor:(UIButton *)sender {
if (sender.isSelected == NO)
{
sender.selected =YES;
sender.backgroundColor = [UIColor greenColor];
}
else
{
sender.selected =NO;
sender.backgroundColor = [UIColor blackColor];
}
}
and if you making it programmtically
UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 25, 25)];
[btn addTarget:self action:@selector(changeColor:) forControlEvents:UIControlEventTouchUpInside];
and implement above method.
Upvotes: 0
Reputation: 516
In you header file write :-
@property (weak, nonatomic) IBOutlet UIButton *myBtn;
- (IBAction)onTapBtn:(id)sender;
In your .m file synthesize it :-
@synthesize myBtn;
then,
-(void)viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
[myBtn setBackgroundColor:[UIColor grayColor]];
count = 0;
}
- (IBAction)onTapBtn:(id)sender {
if (count == 0) {
[myBtn setBackgroundColor:[UIColor greenColor]];
count++;
}else if(count == 1){
[myBtn setBackgroundColor:[UIColor blueColor]];
count = 0;
}
}
Thats it,
You can change the color whatever you want and on how many clicks you want, Hope it helps else you can ask me :)
Upvotes: 0
Reputation: 581
You can create a int property to check the state of the button and increment it.
So, here is how my viewController.h looks like:
#import <UIKit/UIKit.h>
enum clickedTimes{
neverClicked,
clickedOnce,
clickedTwice
};
@interface ViewController : UIViewController
@property int cTimes;
@property (weak, nonatomic) IBOutlet UIButton *myButton;
- (IBAction)changeColour:(id)sender;
@end
I just create a enum to represent the states, and create the button outlet, also it's action.
My viewController.m:
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
//Init the _cTimes property
_cTimes = neverClicked;
}
- (IBAction)changeColour:(id)sender {
switch (_cTimes) {
case neverClicked:
_myButton.tintColor = [UIColor greenColor];
_cTimes++;
break;
case clickedOnce:
_myButton.tintColor = [UIColor blackColor];
_cTimes++;
break;
default:
break;
}
}
Hope it helps. :)
Upvotes: 0