xcode 是否可以更改 UIButton 的操作?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8784545/
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
Is it Possible to Change Action For UIButton?
提问by Musthafa P P
I am trying to change Action For the UIButton in ios applicatio. I did Following Code
我正在尝试更改 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];
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?
不幸的是这个代码说明工作?
回答by Kumaran
I suggest before adding new target, first invoke removeTarget
on UIbutton
object then add new target with other action.
我建议增加新的目标之前,首先调用removeTarget
的UIbutton
对象,然后添加新的目标与其他行动。
回答by Atanu Mondal
I think it will help you
我想它会帮助你
[yourButton removeTarget:nil
action:NULL
forControlEvents:UIControlEventAllEvents];
[yourButton addTarget:self
action:@selector(yourAction:)
forControlEvents:UIControlEventTouchUpInside];
回答by KAREEM MAHAMMED
You can use same action target instead of using two target. In one target you have to differentiate like below
您可以使用相同的动作目标而不是使用两个目标。在一个目标中,您必须区分如下
-(void)btnAction
{
if(target1)
{
// code for target 1
}
else
{
// code for target 2
}
}
Here target1 is BOOL
value which value first set to be YES
. And change its value NO
whenever you want to perform target 2 code.
这里的 target1 是BOOL
值首先设置为 的值YES
。并NO
在您想要执行目标 2 代码时更改其值。
I hope this will helps You.
我希望这会帮助你。
回答by mauricioconde
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
.
我向 中添加了一个标签,
button
并将其与button
需要调用的函数之一相关联(aMethodShow:
例如)。button
总是调用相同的函数(callSpecificFunc:
例如)。什么callSpecificFunc:
是调用函数aMethodShow:
或aMethodHide
根据当前button
标签调用。在
button
需要调用不同函数的特定部分中,我只更改了button
.
Something like this:
像这样的东西:
NSInteger tagOne = 1000; //tag associated with 'aMethodShow' func
NSInteger tagTwo = 1001; //tag associated with 'aMethodHide' func
button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button addTarget:self action:@selector(callSpecificFunc:) forControlEvents:UIControlEventTouchDown];
[button setTitle:@"Show View" forState:UIControlStateNormal];
button.frame = CGRectMake(80.0, 210.0, 160.0, 40.0);
[view addSubview:button];
...
// in some part of code, if we want to call 'aMethodShow' function,
// we set the button's tag like this
button.tag = tagOne
...
//Now, if we want to call 'aMethodHide', just change the button's tag
button.tag = tagTwo
...
-(void) callSpecificFunc:(UIButton*)sender
{
NSInteger tagOne = 1000;
NSInteger tagTwo = 1001;
if([sender tag] == tagOne){
//do whatever 'aMethodShow' does
}else {
//do whatever 'aMethodHide' does
}
}
Of course it could be applied for more than 2 functions :)
当然它可以应用于两个以上的功能:)