IOS:一个 IBAction 用于多个按钮
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5858247/
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
IOS: one IBAction for multiple buttons
提问by cyclingIsBetter
In my project I must control action of 40 buttons, but I don't want to create 40 IBAction, can I use only a IBAction, how?
在我的项目中我必须控制40个按钮的动作,但我不想创建40个IBAction,我可以只使用一个IBAction,如何?
回答by John Parker
If you're using interface builder to create the buttons, simply point them at the same IBAction in the relevant class.
如果您使用界面构建器来创建按钮,只需将它们指向相关类中的同一个 IBAction。
You can then differentiate between the buttons within the IBAction method either by reading the text from the button...
然后,您可以通过从按钮读取文本来区分 IBAction 方法中的按钮...
- (IBAction)buttonClicked:(id)sender {
NSLog(@"Button pressed: %@", [sender currentTitle]);
}
...or by setting the tag
property in Xcode and reading it back via [sender tag]
. (If you use this approach, start the tags at 1, as 0 is the default and hence of little use.)
...或者通过tag
在 Xcode 中设置属性并通过[sender tag]
. (如果您使用这种方法,请从 1 开始标记,因为 0 是默认值,因此很少使用。)
回答by BigAppleBump
-(IBAction)myButtonAction:(id)sender {
if ([sender tag] == 0) {
// do something here
}
if ([sender tag] == 1) {
// Do something here
}
}
// in Other words
-(IBAction)myButtonAction:(id)sender {
switch ([sender tag]) {
case 0:
// Do something here
break;
case 1:
// Do something here
break;
default:
NSLog(@"Default Message here");
break;
}
回答by Caleb
Set all the buttons to use that one action. Actions generally have a sender
parameter, which you can use to figure out which button is calling the action. One popular way to tell the difference between buttons is to assign a different value to each button's tag
property. So you might have 40 buttons with tags ranging from 1 to 40. (0 probably isn't a great choice for a tag since that's what the default value is, and any button for which you forget to set the tag will have 0 as the tag value.)
设置所有按钮以使用该操作。动作通常有一个sender
参数,您可以使用它来确定哪个按钮正在调用该动作。区分按钮的一种流行方法是为每个按钮的tag
属性分配不同的值。所以你可能有 40 个按钮,标签的范围从 1 到 40。(0 可能不是一个很好的标签选择,因为这是默认值,任何你忘记设置标签的按钮都会有 0 作为标签值。)
This technique is most useful when all the buttons do approximately the same thing, like the buttons on a calculator or keyboard. If each of the buttons does something completely different, then you still end up with the equivalent of 40 methods, but you substitute your own switch statement for Objective-C's messaging system. In that case, it's often better just to spend the time to create as many actions as you need an assign them appropriately.
当所有按钮都执行大致相同的操作时,此技术最有用,例如计算器或键盘上的按钮。如果每个按钮都做完全不同的事情,那么您最终仍然会得到 40 个等价的方法,但是您可以用自己的 switch 语句替换 Objective-C 的消息传递系统。在这种情况下,通常最好花时间创建尽可能多的操作,并适当地分配它们。
回答by Ole Begemann
Sure. Just connect all buttons to the same action method in Interface Builder. Use the method's sender
argument (possibly in conjunction with the buttons' tag
property) to identify which button is sending the event.
当然。只需将所有按钮连接到 Interface Builder 中的相同操作方法。使用方法的sender
参数(可能与按钮的tag
属性结合使用)来确定哪个按钮正在发送事件。
回答by Joris Mans
Just use one IBAction and assign it to all your buttons.
只需使用一个 IBAction 并将其分配给您的所有按钮。
回答by clive dancey
Just used the above method myself , had a selection of buttons but converted them all and used switch case instead
我自己刚刚使用了上述方法,选择了一些按钮,但将它们全部转换并使用开关盒代替
-(IBAction)buttons:(id)sender
{
switch ([sender tag])
{
case 0 :
}
}
回答by misatota
Seems like you are getting all the answers you need, but I wanted to add to everyone else's answers.
似乎你得到了你需要的所有答案,但我想添加到其他人的答案中。
Whether you want to use one IBAction or 40 actions is up to what you want the buttons to do. If all the buttons do completely different things, you need all separate IBActions, but if you want all of them to do the same thing, you can use just one. I need more details of those buttons and action, but you probably have title for each button, so you can use that to differentiate each button and create a message or something that's being customized by the particular button pressed. Here is the example. Each time a button is pressed, a label displays a message that say "i.e.title of the button" pressed.
您想使用一个 IBAction 还是 40 个操作取决于您希望按钮做什么。如果所有按钮都做完全不同的事情,您需要所有独立的 IBActions,但如果您希望所有按钮都做同样的事情,您可以只使用一个。我需要这些按钮和操作的更多详细信息,但您可能有每个按钮的标题,因此您可以使用它来区分每个按钮并创建消息或由按下的特定按钮自定义的内容。这是示例。每次按下按钮时,标签都会显示一条消息,说明“按钮的标题”已按下。
By doing it this way, you don't need to do switch case with all 40 patterns. You can still display or do something that's individualized by the button pressed with just 2-3 lines of code.
通过这样做,您不需要对所有 40 种模式进行 switch case。只需 2-3 行代码,您仍然可以通过按下按钮来显示或执行一些个性化的操作。
- (IBAction)button_Clicked:(UIButton *)sender {
//Get the buttons' titles.
NSString *title =[sender titleForState:UIControlStateNormal];
//Construct a message that includes the *title.
NSString *plainText=[NSString stringWithFormat:@"%@ button pressed.", title];
//Assigns the *plainText to the label.
self.Label.text=plainText;
}
@end