是否可以更改 UIButton 的操作?
我正在尝试更改 ios 应用程序中 UIButton 的操作。我在特定部分中执行了以下代码
button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button addTarget:self action:@selector(aMethodShow:) forControlEvents:UIControlEventTouchDown];
[button setTitle:@"Show View" forState:UIControlStateNormal];
button.frame = CGRectMake(80.0, 210.0, 160.0, 40.0);
[view addSubview:button];
,我想更改此按钮的操作。所以我这样做了
[button addTarget:self action:@selector(aMethodHide:) forControlEvents:UIControlEventTouchDown];
[button setTitle:@"Hide View" forState:UIControlStateNormal];
不幸的是此代码注释工作吗?
I am trying to change Action For the UIButton in ios applicatio. I did Following Code
button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button addTarget:self action:@selector(aMethodShow:) forControlEvents:UIControlEventTouchDown];
[button setTitle:@"Show View" forState:UIControlStateNormal];
button.frame = CGRectMake(80.0, 210.0, 160.0, 40.0);
[view addSubview:button];
In particular Section I want to change Action for this Button .So i did this
[button addTarget:self action:@selector(aMethodHide:) forControlEvents:UIControlEventTouchDown];
[button setTitle:@"Hide View" forState:UIControlStateNormal];
Unfortunately This code note Working?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我建议在添加新目标之前,首先在
UIbutton
对象上调用removeTarget
,然后通过其他操作添加新目标。I suggest before adding new target, first invoke
removeTarget
onUIbutton
object then add new target with other action.我想这会对你有帮助
I think it will help you
您可以使用相同的操作目标,而不是使用两个目标。在一个目标中,您必须像下面这样区分
这里 target1 是
BOOL
值,该值首先设置为YES
。每当您想要执行目标 2 代码时,请更改其值NO
。我希望这会对您有所帮助。
You can use same action target instead of using two target. In one target you have to differentiate like below
Here target1 is
BOOL
value which value first set to beYES
. And change its valueNO
whenever you want to perform target 2 code.I hope this will helps You.
我最近制作了一个应用程序,也遇到了同样的情况,但我找到了另一种方法来解决它,所以我决定与可能处于相同情况的人分享我的解决方案。
我将尝试解释我在这个问题的上下文中做了什么:
我向
button
添加了一个标签,并将其与button
的功能之一关联起来> 需要调用(例如aMethodShow:
)。button
始终调用相同的函数(例如callSpecificFunc:
)。callSpecificFunc:
的作用是根据当前button
标签调用函数aMethodShow:
或aMethodHide
。在
按钮
需要调用不同函数的特定部分中,我仅更改按钮
的标签。像这样的东西:
当然它可以应用于两个以上的功能:)
I made an app recently and i had the same situation, but i found another way to solve it so i decided to share my solution with people who may be in the same situation.
I'll try to explain what i did with the context of this question:
I added a tag to the
button
and i associated it with one of the functions thatbutton
needs to call (aMethodShow:
for example).button
always call the same function (callSpecificFunc:
for example). WhatcallSpecificFunc:
does is call either functionaMethodShow:
oraMethodHide
according with the currentbutton
tag.In the particular section in which the
button
needs to call a different function, i only change the tag ofbutton
.Something like this:
Of course it could be applied for more than 2 functions :)