我和NSUserDefaults一起使用,即使它不是我要求的意思 如果我在该按钮上单击一次,它应该将颜色更改为“绿色”,如果我再按一次它应该将颜色更改为黑色....
这里我的代码是......
- (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;
}
}
在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 ");
}
}
答案 0 :(得分:0)
您可以创建一个int属性来检查按钮的状态并将其递增。
所以,这是我的viewController.h的样子:
#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
我只是创建一个枚举来表示状态,并创建按钮插座,也是它的动作。
我的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;
}
}
希望它有所帮助。 :)
答案 1 :(得分:0)
在你的头文件中写: -
@property (weak, nonatomic) IBOutlet UIButton *myBtn;
- (IBAction)onTapBtn:(id)sender;
在你的.m文件中合成它: -
@synthesize myBtn;
然后,
-(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;
}
}
多数民众赞成,
您可以根据需要更改颜色以及您想要的点击次数,希望它能帮助您,除此之外您可以问我:)
答案 2 :(得分:0)
希望这会对你有所帮助
如果您通过.xib文件创建,则执行类似
的操作事件- (IBAction)changeColor:(UIButton *)sender {
if (sender.isSelected == NO)
{
sender.selected =YES;
sender.backgroundColor = [UIColor greenColor];
}
else
{
sender.selected =NO;
sender.backgroundColor = [UIColor blackColor];
}
}
如果你以编程方式制作
UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 25, 25)];
[btn addTarget:self action:@selector(changeColor:) forControlEvents:UIControlEventTouchUpInside];
并实现上述方法。
答案 3 :(得分:0)
按钮操作
- (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];
}
}