gpt4 book ai didi

ios - 如何存储所有选定的按钮值并在 UILabel 上显示它们?

转载 作者:行者123 更新时间:2023-11-29 02:43:19 24 4
gpt4 key购买 nike

其实我只是想把它变成这样:

当用户点击按钮 1 时,它将其值存储在一个字符串中,当用户选择另一个按钮时;它还存储它的值,当他再次取消选择按钮时,它必须从字符串和 uiLabel 中删除

这怎么可能?

我必须知道所有可能的方法

最佳答案

1) 在您的 ViewController.m

中声明
@interface ViewController ()
{
UIButton *button1;
UIButton *button2;

NSString *btn1String;
NSString *btn2String;

BOOL btn1isClicked;
BOOL btn2isClicked;

UILabel *label;
}

2) 按钮和标签(可以在你的ViewDidLoad中)

btn1isClicked = NO;
btn2isClicked = NO;

//initialy set to nothing
btn1String = @"";
btn2String = @"";

//create first button
button1 = [[UIButton alloc] initWithFrame:CGRectMake(0, 50, 200, 50)];
//button 1 clicked
[button1 addTarget:self action:@selector(button1Clicked) forControlEvents:UIControlEventTouchUpInside];
[button1 setTitle:@"Button One" forState:UIControlStateNormal];
[button1 setTitleColor:[UIColor purpleColor] forState:UIControlStateNormal];
[self.view addSubview:button1];

//create second button
button2 = [[UIButton alloc] initWithFrame:CGRectMake(0, 150, 200, 50)];
//button 2 clicked
[button2 addTarget:self action:@selector(button2Clicked) forControlEvents:UIControlEventTouchUpInside];
[button2 setTitle:@"Button Two" forState:UIControlStateNormal];
[button2 setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
[self.view addSubview:button2];

//create label
label = [[UILabel alloc] initWithFrame:CGRectMake(0, 300, 320, 50)];
[label setText:@""];
[self.view addSubview:label];

3) 按钮点击方法

- (void) button1Clicked
{
if (!btn1isClicked)
{
//set bool to yes
btn1isClicked = YES;
[button1 setTitle:@"Button One Clicked" forState:UIControlStateNormal];
//set button 1 value
btn1String = @"btn1value";
}
else
{
//set bool to no
btn1isClicked = NO;
[button1 setTitle:@"Button One" forState:UIControlStateNormal];
btn1String = @"";
}
//update label
[label setText:[NSString stringWithFormat:@"%@ %@", btn1String, btn2String]];
}

- (void) button2Clicked
{
if (!btn2isClicked)
{
btn2isClicked = YES;
[button2 setTitle:@"Button Two Clicked" forState:UIControlStateNormal];
//set button 2 value
btn2String = @"btn2value";
}
else
{
btn2isClicked = NO;
[button2 setTitle:@"Button Two" forState:UIControlStateNormal];
btn2String = @"";
}
//update label
[label setText:[NSString stringWithFormat:@"%@ %@", btn1String, btn2String]];
}

希望对你有帮助

关于ios - 如何存储所有选定的按钮值并在 UILabel 上显示它们?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25471504/

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