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

Posted

技术标签:

【中文标题】如何存储所有选定的按钮值并在 UILabel 上显示它们?【英文标题】:How to store all selected buttons values and show them on UILabel? 【发布时间】:2014-08-24 12:04:53 【问题描述】:

其实我只是想让它像:

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

这怎么可能?

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

【问题讨论】:

【参考方案1】:

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]];

希望对你有帮助

【讨论】:

【参考方案2】:
-(IBActions)buttonDidTap:(UIButton*)btn   //the target selector the button is attached to


   if ([btn isSelected])  //check if the button is selected

      NSString *buttonTitleValue = btn.currentTitle; //Get the button title value
      myLabel.text = buttonTitleValue; //set the UIlabel text to the title of the button 

   



见:https://developer.apple.com/library/ios/documentation/uikit/reference/UIButton_Class/UIButton/UIButton.html#//apple_ref/occ/instp/UIButton/currentTitle

【讨论】:

以上是关于如何存储所有选定的按钮值并在 UILabel 上显示它们?的主要内容,如果未能解决你的问题,请参考以下文章

如何在 asp.net 代码中获取引导文本框值并在存储过程中作为参数发送?

保存下拉列表的选定值并在另一个下拉列表/重复下拉列表中默认分配它

Vue中如何存储多个children的值并在父组件中执行一个函数

将选定对象传递给第二个视图控制器并在 uilabel 中显示

获取不同视图中存在的选择器视图的选定值并在其他视图中使用字符串

如何比较两个for循环的值并在django模板中使用if语句?