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

时间:2014-08-24 12:04:54

标签: ios iphone uibutton uilabel multipleselection

实际上我只想把它变成:

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

怎么可能?

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

2 个答案:

答案 0 :(得分: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]];
}

希望这有帮助

答案 1 :(得分:0)

-(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