簡體   English   中英

如何存儲所有選定按鈕的值並在UILabel上顯示它們?

[英]How to store all selected buttons values and show them on UILabel?

實際上,我只想使其如下所示:

當用戶點擊按鈕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]];
}

希望這可以幫助

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM