将不同的参数传递给IBAction [英] Pass different parameters to an IBAction

查看:186
本文介绍了将不同的参数传递给IBAction的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的iPhone应用程式有许多按钮,我想让所有的按钮调用相同的方法,但使用不同的参数。

My iPhone app has many buttons and I want all the buttons to call the same method, but with different parameters.

例如我要点击一个按钮方法 myMethod:与参数 @foo,第二个按钮应该调用相同的方法 @bar

For example I want tapping one button to call the method myMethod: with the argument @"foo", and a second button should call the same method but with argument @"bar".

推荐答案

具有以下签名之一:

-(void)action;
-(void)actionWithSender:(id)sender;
-(void)actionWithSender:(id)sender event:(UIEvent*)event;

您不能添加任何其他参数。不过,您可以使用 sender (在您的情况下是button1或button2)来获得参数:

You cannot add any other parameters. Nevertheless you can use sender (which is button1 or button2 in your case) to get the parameter:

-(void)actionWithSender:(UIButton*)sender {
   NSString* parameter;
   if (sender.tag == 1)   // button1
     parameter = @"foo";
   else                   // button2
     parameter = @"bar";
   ...
}

这篇关于将不同的参数传递给IBAction的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆