将不同的参数传递给 IBAction
我的 iPhone 应用程序有很多按钮,我希望所有按钮都调用相同的方法,但参数不同。
例如,我想点击一个按钮来调用带有参数 @"foo"
的方法 myMethod:
,第二个按钮应该调用相同的方法,但带有参数 @“酒吧”
。
My iPhone app has many buttons and I want all the buttons to call the same method, but with different parameters.
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"
.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
所谓的“IBActions”必须具有以下签名之一:
您不能添加任何其他参数。不过,您可以使用
sender
(在您的情况下为button1或button2)来获取参数:The so called "IBActions" must have one of these signatures:
You cannot add any other parameters. Nevertheless you can use
sender
(which is button1 or button2 in your case) to get the parameter:您无法添加额外参数的真正原因是 UIKIT 会将参数推送到堆栈上。
所以唯一的方法就是使用标签。
一种肮脏的方法是将指针转换为 int 并用它标记按钮:
注意:参数必须保持静态..或使用 malloc 分配(越来越难看的代码...)。
不要使用本地变量:它将在堆栈上分配,因此在退出设置方法后将被删除。
the real reason You cannot add additional parameter is that UIKIT will push params on the stack.
so the only way is to use tags.
A DIRTY way can be to convert a pointer to int and tagging the button with it:
NOTE: params MUST be keep static .. or allocate using malloc (more and more ugly code...).
DO NOT use a local var: it will be allocated on stack so will be removed after exiting from the setup method.
为您的各种
UIButton
实例提供不同的tag
属性值。在您的
IBAction
方法-myMethod:
中,您可以执行以下操作:值
firstButtonTag
和secondButtonTag
可以是如果您想让它易于维护,请存储在enum
中。Give your various
UIButton
instances differenttag
property values.In your
IBAction
method-myMethod:
, you might then do something like:The values
firstButtonTag
andsecondButtonTag
can be stored in anenum
if you want to make this easy to maintain.您不能通过 IBAction 传递参数。我通常做的就是为按钮赋予 IB 中唯一的标签。该标签是一个整数值,因此我然后使用一个简单的查找表将标签转换为某个值。
在本例中,有三个按钮,但标签为 1 到 3:
You can't pass parameters through an IBAction. What I usually do is give the buttons the unique tag in IB. THe tag is an integer value so I u then use a simple lookup table to convert the tag to some value.
In this case, three buttons but tags 1 to 3:
(id)Sender 显示无论您在 UIButton 单击事件上传递什么,都会直接传递给此方法,无论它是什么类型,它都会自动采用,就像您传递按钮标签一样,它会将按钮标签作为 sender.tag 等
(id)Sender is shows that whatever u pass on UIButton click event is directly pass to this method and no matter that what type it is , it take automatically like if you pass button tag then it take button tag as sender.tag etc
正如其他人提到的,您无法将自定义参数传递到操作方法中。如果您不喜欢使用标签的解决方案,您还可以使用自定义类对 UIButton 进行子类化,并在其中添加参数。 (我不会打扰,只使用标签)
As others have mentioned you cannot pass your custom parameter into action method. If you do not like the solution using tags you may also subclass UIButton with your custom class and add your parameter there. (By I wouldn't bother and just use tags)
你不知道。唯一的参数是发送者对象,您可以使用它来产生不同的行为,但我要做的是定义 2 个操作方法,它们只是依次调用具有不同参数的相同方法,即您将拥有
:为了捍卫该方法(没有双关语),我想补充一点,当您使用 Interface Builder 查看 UI 时会更清楚地看到不同的按钮实际上有不同的效果,这更难判断它们是否都调用了whateverAction:
You don't. The only parameter is the sender object, which you may use to have a different behavior, but what I'd do is define 2 action methods, which simply in turn call the same method with a different parameter, i.e. you'd have:
In defense of that method (no pun intended), I'd add that it makes clearer when you review your UI with Interface Builder to see that different buttons actually have different effects, which is harder to tell if they all call whateverAction: