2

我有很多按钮,并且我给了所有的标签值。每当我单击一个按钮时,我都需要获取该单击按钮的相关标签,相关标签表示上、下、右和左。

我想存储第一个放置的标签,并与下一个放置的标签进行比较,无论它在哪个位置。

怎么做?

请帮助我。

4

3 回答 3

2

In your *.h file declare an action like:

-(IBAction)buttonTapped:(UIButton *)sender;

Assign the "Touch Up Inside" action to this action.

In your *.m file do the following:

-(IBAction)buttonTapped:(UIButton *)sender
{
    switch(sender.tag) {
        case 1: // action for button 1
        case 2: // action for button 2
        ...
    }
}

Note that tag is an integer number, not a string.

于 2013-04-21T16:04:43.747 回答
1

只需登录并查看您单击的按钮的标签。

-(IBAction)buttonClicked:(UIButton *)sender
{
   int tag=sender.tag;
   NSLog(@"%d",tag);

}
于 2013-04-21T19:11:12.423 回答
1

您可以添加该按钮的目标

[buttonName addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];

-(void)buttonClicked:(UIButton *)sender
{
   int tag=sender.tag;

   UIButton *btn = (UIButton *)sender;  
   //Now you can access all he properties of that button as well as you can change it's property whatever you like.
   int tag = btn.tag;
}
于 2013-04-22T06:38:30.977 回答