我是iOS的初学者,在我的项目中我已经为用户“检查”和“取消选中”按钮插入了“3”按钮,如下图所示..
在这里,我的主要要求是当我点击“选中”按钮,然后它应该是“未选中”,当我点击“未选中”按钮时,它应该被“检查”,如下图所示..
剩下的两个按钮必须“未选中”,为此,我写了下面的代码,但这个过程太长了。任何人都可以解释简单和简短的过程
@interface ViewController ()
{
int check1;
int check2;
int check3;
UIButton * button1;
UIButton * button2;
UIButton * button3;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
check1 = 0;
check2 = 0;
check3 = 0;
button1 = [UIButton buttonWithType:UIButtonTypeCustom];
[button1 addTarget:self
action:@selector(aMethod1:)
forControlEvents:UIControlEventTouchUpInside];
button1.frame = CGRectMake(10.0, 100.0, 30.0, 30.0);
UIImage *btnImage1 = [UIImage imageNamed:@"uncheck.png"];
[button1 setImage:btnImage1 forState:UIControlStateNormal];
[self.view addSubview:button1];
button2 = [UIButton buttonWithType:UIButtonTypeCustom];
[button2 addTarget:self
action:@selector(aMethod2:)
forControlEvents:UIControlEventTouchUpInside];
button2.frame = CGRectMake(50.0, 100.0, 30.0, 30.0);
UIImage *btnImage2 = [UIImage imageNamed:@"uncheck.png"];
[button2 setImage:btnImage2 forState:UIControlStateNormal];
[self.view addSubview:button2];
button3 = [UIButton buttonWithType:UIButtonTypeCustom];
[button3 addTarget:self
action:@selector(aMethod3:)
forControlEvents:UIControlEventTouchUpInside];
button3.frame = CGRectMake(100.0, 100.0, 30.0, 30.0);
UIImage *btnImage3 = [UIImage imageNamed:@"uncheck.png"];
[button3 setImage:btnImage3 forState:UIControlStateNormal];
[self.view addSubview:button3];
}
-(void)aMethod1 :(id)sender{
if (check1 == 0) {
UIImage *btnImage1 = [UIImage imageNamed:@"check.png"];
[button1 setImage:btnImage1 forState:UIControlStateNormal];
check1 = 1;
}
else{
UIImage *btnImage1 = [UIImage imageNamed:@"uncheck.png"];
[button1 setImage:btnImage1 forState:UIControlStateNormal];
check1 = 0;
}
}
-(void)aMethod2 :(id)sender{
if (check2 == 0) {
UIImage *btnImage1 = [UIImage imageNamed:@"check.png"];
[button2 setImage:btnImage1 forState:UIControlStateNormal];
check2 = 1;
}
else{
UIImage *btnImage1 = [UIImage imageNamed:@"uncheck.png"];
[button2 setImage:btnImage1 forState:UIControlStateNormal];
check2 = 0;
}
}
-(void)aMethod3 :(id)sender{
if (check3 == 0) {
UIImage *btnImage1 = [UIImage imageNamed:@"check.png"];
[button3 setImage:btnImage1 forState:UIControlStateNormal];
check3 = 1;
}
else{
UIImage *btnImage1 = [UIImage imageNamed:@"uncheck.png"];
[button3 setImage:btnImage1 forState:UIControlStateNormal];
check3 = 0;
}
}
答案 0 :(得分:2)
请喜欢。它如此简单
UIButton *button1 = [UIButton buttonWithType:UIButtonTypeCustom];
[button1 addTarget:self
action:@selector(aMethod1:)
forControlEvents:UIControlEventTouchUpInside];
button1.frame = CGRectMake(10.0, 100.0, 30.0, 30.0);
UIImage *btnImage1 = [UIImage imageNamed:@"uncheck.png"];
UIImage *selected = [UIImage imageNamed:@"check.png"];
[button1 setImage:btnImage1 forState:UIControlStateNormal];
[button1 setImage:selected forState:UIControlStateSelected];
button1.selected = NO;
- (void)aMethod1:(UIButton *)btn{
btn.selected = !btn.selected;
}
答案 1 :(得分:1)
试试下面的Code,它的简单解决方案。
BOOL checked;
-(void)viewDidLoad {
checked = YES; //Set depending on your initial state
}
-(IBACtion)pressed:(UIButton *)sender {
checked = !checked;
[sender setImage:[UIImage imageNamed:((checked) ? @"check2.png" : @"NOTCHECKEDIMAGE.png")] forState:UIControlStateNormal];
}
可能对您有所帮助。
// when you setup your button,eg. in Your ViewDidLoad method set an image for the selected and normal states
[myCheckBoxButton setImage:nonCheckedImage forState:UIControlStateSelected];
[myCheckBoxButton setImage:nonCheckedImage forState:UIControlStateNormal];
- (void)myCheckboxToggle:(id)sender
{
myCheckboxButton.selected = !myCheckboxButton.selected; // toggle the selected property, just a simple BOOL
}
这里也有Diff Library,你也可以尝试一下
答案 2 :(得分:1)
[_nameTextField resignFirstResponder];
[_emailTextField resignFirstResponder];
[_contactNumberTextField resignFirstResponder];
if (sender.tag==0)
{
sender.tag = 1;
[sender setImage:[UIImage imageNamed:@"uncheck.png"] forState:UIControlStateNormal];
}
else
{
sender.tag=0;
[sender setImage:[UIImage imageNamed:@"check.png"] forState:UIControlStateNormal];
}
答案 3 :(得分:1)
试试这个,会帮助你并轻松实现
@interface ViewController ()
{
int check1;
int check2;
int check3;
UIButton * button1;
UIButton * button2;
UIButton * button3;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
check1 = 0;
check2 = 0;
check3 = 0;
button1 = [UIButton buttonWithType:UIButtonTypeCustom];
[button1 addTarget:self
action:@selector(checkNdUncheckButtons:)
forControlEvents:UIControlEventTouchUpInside];
button1.tag = 100;
button1.frame = CGRectMake(10.0, 100.0, 30.0, 30.0);
UIImage *btnImage1 = [UIImage imageNamed:@"uncheck.png"];
[button1 setImage:btnImage1 forState:UIControlStateNormal];
[self.view addSubview:button1];
button2 = [UIButton buttonWithType:UIButtonTypeCustom];
[button2 addTarget:self
action:@selector(checkNdUncheckButtons:)
forControlEvents:UIControlEventTouchUpInside];
button2.tag = 200;
button2.frame = CGRectMake(50.0, 100.0, 30.0, 30.0);
UIImage *btnImage2 = [UIImage imageNamed:@"uncheck.png"];
[button2 setImage:btnImage2 forState:UIControlStateNormal];
[self.view addSubview:button2];
button3 = [UIButton buttonWithType:UIButtonTypeCustom];
[button3 addTarget:self
action:@selector(checkNdUncheckButtons:)
forControlEvents:UIControlEventTouchUpInside];
button3.tag = 300;
button3.frame = CGRectMake(100.0, 100.0, 30.0, 30.0);
UIImage *btnImage3 = [UIImage imageNamed:@"uncheck.png"];
[button3 setImage:btnImage3 forState:UIControlStateNormal];
[self.view addSubview:button3];
}
-(void)checkNdUncheckButtons :(id)sender{
UIButton *selectedButton = (UIButton*)sender;
UIImage *checkimage = [UIImage imageNamed:@"check.png"];
UIImage *unCheckImage = [UIImage imageNamed:@"uncheck.png"];
if (selectedButton.tag == 100) {
if (button1.image == checkimage) {
[button1 setImage:unCheckImage forState:UIControlStateNormal];
} else {
[button1 setImage:checkimage forState:UIControlStateNormal];
}
[button2 setImage:unCheckImage forState:UIControlStateNormal];
[button3 setImage:unCheckImage forState:UIControlStateNormal];
}
else if (selectedButton.tag == 200){
[button1 setImage:unCheckImage forState:UIControlStateNormal];
if (button2.image == checkimage) {
[button2 setImage:unCheckImage forState:UIControlStateNormal];
}
else {
[button2 setImage:checkimage forState:UIControlStateNormal];
}
[button3 setImage:unCheckImage forState:UIControlStateNormal];
}
else {
[button1 setImage:unCheckImage forState:UIControlStateNormal];
[button2 setImage:unCheckImage forState:UIControlStateNormal];
if (button3.image == checkimage) {
[button3 setImage:unCheckImage forState:UIControlStateNormal];
}
else {
[button3 setImage:checkimage forState:UIControlStateNormal];
}
}
尝试理解然后在代码中实现然后执行
答案 4 :(得分:1)
请在代码中尝试以下修正。
@interface ViewController ()
{
UIButton * button1;
UIButton * button2;
UIButton * button3;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
UIImage *btnImage1 = [UIImage imageNamed:@"uncheck.png"];
UIImage *btnImage2 = [UIImage imageNamed:@"check.png"];
button1 = [UIButton buttonWithType:UIButtonTypeCustom];
[button1 addTarget:self action:@selector(aMethod1:)
forControlEvents:UIControlEventTouchUpInside];
button1.frame = CGRectMake(10.0, 100.0, 30.0, 30.0);
UIButton *button1 = [UIButton buttonWithType:UIButtonTypeCustom];
[button1 setImage:btnImage1 forState:UIControlStateNormal];
[button1 setImage:btnImage2 forState:UIControlStateSelected];
button1.selected = NO;
[self.view addSubview:button1];
button2 = [UIButton buttonWithType:UIButtonTypeCustom];
[button2 addTarget:self action:@selector(aMethod2:)
forControlEvents:UIControlEventTouchUpInside];
button2.frame = CGRectMake(50.0, 100.0, 30.0, 30.0);
[button2 setImage:btnImage1 forState:UIControlStateNormal];
[button2 setImage:btnImage2 forState:UIControlStateSelected];
button2.selected = NO;
[self.view addSubview:button2];
button3 = [UIButton buttonWithType:UIButtonTypeCustom];
[button3 addTarget:self action:@selector(aMethod3:)
forControlEvents:UIControlEventTouchUpInside];
button3.frame = CGRectMake(100.0, 100.0, 30.0, 30.0);
[button3 setImage:btnImage1 forState:UIControlStateNormal];
[button3 setImage:btnImage2 forState:UIControlStateSelected];
button3.selected = NO;
[self.view addSubview:button3];
}
-(void)aMethod1 :(id)sender {
button1.selected = !button1.selected;
}
-(void)aMethod2 :(id)sender {
button2.selected = !button2.selected;
}
-(void)aMethod3 :(id)sender {
button3.selected = !button3.selected;
}
谢谢:)
答案 5 :(得分:1)
首先将图像重命名为check_0.png(未检查图像)& check_1.png(选中图片)
@interface HomeViewController () {
NSMutableArray *selctionArray;
}
[self initArrayData];
-(void) initArrayData {
selctionArray = [[NSMutableArray alloc] init];
for (int i=0;i<3;i++) {
[selctionArray addObject:@"0"];
}
}
首先创建三个按钮,以编程方式调用clikcedButton
作为点击侦听器。 Say按钮是button1,按钮2,按钮3。
button1.tag = 123451;
button2.tag = 123452;
button3.tag = 123453;
// by-default first button will be checked
[selctionArray replaceObjectAtIndex:0 withObject:@"1"];
[self updateMyButtons];
-(void) updateMyButtons {
UIButton *mButton;
for (int i=0;i<3;i++) {
mButton = (UIButton *)[self.view viewWithTag:123451+i];
[mButton setBackgroundImage:[UIImage imageNamed:[NSString stringWithFormat:@"check_%@.png", [selctionArray objectAtIndex:i]] forState:UIControlStateNormal];
}
}
-(IBAction)clikcedButton:(id)sender {
[self initArrayData];
UIButton *mButton = (UIButton *) sender;
int clickedButtonTag = mButton.tag;
clickedButtonTag = clickedButtonTag - 123451;
[selctionArray replaceObjectAtIndex:clickedButtonTag withObject:@"1"];
[self updateMyButtons];
}
如果您还有其他需要,请告诉我。