gpt4 book ai didi

ios - 如何检查和取消选中ios中的按钮

转载 作者:行者123 更新时间:2023-12-01 17:29:42 30 4
gpt4 key购买 nike

我是iOS的初学者,在我的项目中,我为用户“选中”和“取消选中”按钮插入了“3”按钮,如下图所示。

在这里,我的主要要求是当我单击“选中”按钮时,则应将其“取消选中”,而当我单击“未选中”按钮时,则应将其选中,如下图所示。

enter image description here

剩下的两个按钮必须“未选中”,为此,我在下面的代码中进行了编写,但这是一个漫长的过程。任何人都可以解释简单和简短的过程

我的代码:

 @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;
    }
}

最佳答案

首先将图像重命名为check_0.png(未选中的图像)和check_1.png(选中的图像)

步骤1:在界面中声明一个全局变量

@interface HomeViewController () {
NSMutableArray *selctionArray;
}

步骤2:在viewDidLoad中初始化此数组并添加数据
[self initArrayData];

-(void) initArrayData {
selctionArray = [[NSMutableArray alloc] init];
for (int i=0;i<3;i++) {
[selctionArray addObject:@"0"];
}
}

步骤3:以编程方式创建3个按钮,并为其设置标签

首先创建三个按钮,以编程方式调用 clikcedButton作为Click侦听器。说按钮是按钮1,按钮2,按钮3。
button1.tag = 123451;
button2.tag = 123452;
button3.tag = 123453;

步骤4:假设第一个按钮已选中
// 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];
}
}

步骤5:现在让我们实现IBAction
-(IBAction)clikcedButton:(id)sender {
[self initArrayData];
UIButton *mButton = (UIButton *) sender;
int clickedButtonTag = mButton.tag;
clickedButtonTag = clickedButtonTag - 123451;
[selctionArray replaceObjectAtIndex:clickedButtonTag withObject:@"1"];
[self updateMyButtons];
}

需要帮助请叫我。

关于ios - 如何检查和取消选中ios中的按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33211577/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com