ios 在 iPhone 中获取按钮标签值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7282809/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 21:22:24  来源:igfitidea点击:

Getting Button Tag Value in iPhone

iosuibutton

提问by Sanjay

I have made 20 Buttons dynamically, and I got the tag values of all Buttons.

我已经动态制作了 20 个 Button,并且我得到了所有 Button 的标签值。

But I need to know how to use that tag values.

但我需要知道如何使用该标签值。

I need information on every button pressed with tag values.
So, how do I use those tag values?

我需要有关使用标签值按下的每个按钮的信息。
那么,我该如何使用这些标签值呢?

回答by Filip Radelic

You need to set target-action of each button.

您需要设置每个按钮的目标操作。

[button setTarget:self action:@selector(someMethod:) forControlEvents:UIControlEventTouchUpInside];

Then implement someMethod:like this:

然后someMethod:像这样实现:

- (void)someMethod:(UIButton *)sender {
    if (sender.tag == 1) {
        // do action for button with tag 1
    } else if (sender.tag == 2) {
        // do action for button with tag 2
    } // ... and so on
}

回答by EmptyStack

Why do you need to use the tagto get the button. You can directly get the buttons reference from its action method.

为什么需要使用tag来获取按钮。您可以直接从其操作方法中获取按钮引用。

- (void)onButtonPressed:(UIButton *)button {

    // "button" is the button which is pressed
    NSLog(@"Pressed Button: %@", button);

    // You can still get the tag
    int tag = button.tag;
}

I hope you have added the target-action for the button.

我希望您已经为按钮添加了目标操作。

[button addTarget:self action:@selector(onButtonPressed:) 
             forControlEvents:UIControlEventTouchUpInside];

回答by Akshay

Set the tags like this :

像这样设置标签:

for (createButtonIndex=0; createButtonIndex<buttonsCount; createButtonIndex++) 
    {
buttonCaps.tag=createButtonIndex;
}

And add the method to trap the tags :-

并添加捕获标签的方法:-

-(void)buttonsAction:(id)sender
{
    UIButton *instanceButton = (UIButton*)sender;

switch(instanceButton.tag)
{
case 1(yourTags):
//Code
break;
case 2:
//Code
break;
}
}

Hope this Helps !!

希望这可以帮助 !!

回答by Nekto

You can get reference to that your buttons using that tags. For example, you've added UIButtons to UIView *mainView. To get reference to that buttons you should write following:

您可以使用该标签参考您的按钮。例如,您已将UIButtons添加到UIView *mainView. 要获得对该按钮的引用,您应该编写以下内容:

UIButton *buttonWithTag1 = (UIButton *)[mainView viewWithTag:1];

回答by visakh7

- (IBAction)buttonPressed:(id)sender {
   UIButton selectedButton = (UIButton *)sender;
   NSLog(@"Selected button tag is %d%", selectedButton.tag);
}

回答by Srinivas

    usefully we use btn tag if You Write One Function For (more than one) Buttons .in action if we want to write separate Action For button at that situvation we use btn tag.it can get two ways  

    I) case  sender.tag
    //if we have four buttons Add,mul,sub,div having Same selector and add.tag=10
    mul.tag=20,sub.tag=30,div.tag=40;
    -(IBAction) dosomthing:(id)sender   
    {
    int x=10;
    int y=20;
    int result;
     if(sender.tag==10)
    {
    result=x+y;

    }else if(sender.tag==20)
    {
    result=x*y;

    }else if(sender.tag==30)
    {
    result=x-y;

    }else if(sender.tag==40)
    {
    result=x/y;

    }
    NSLog(@"%i",result);

    }

2)Case
UIButton *btn=[self.view viewWithTag:10];
then you got object of add button uyou can Hide It With btn.hidden=YES;

回答by Bhavesh Nayi

UIButton *btn = (UIButton *)[mainView viewWithTag:button.tag];