是否可以更改 UIButton 的操作?

发布于 2024-12-25 18:53:32 字数 634 浏览 0 评论 0原文

我正在尝试更改 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

无畏 2025-01-01 18:53:32

我建议在添加新目标之前,首先在 UIbutton 对象上调用 removeTarget,然后通过其他操作添加新目标。

I suggest before adding new target, first invoke removeTarget on UIbutton object then add new target with other action.

吾家有女初长成 2025-01-01 18:53:32

我想这会对你有帮助

[yourButton removeTarget:nil 
               action:NULL 
     forControlEvents:UIControlEventAllEvents]; 

[yourButton addTarget:self 
               action:@selector(yourAction:)
     forControlEvents:UIControlEventTouchUpInside];

I think it will help you

[yourButton removeTarget:nil 
               action:NULL 
     forControlEvents:UIControlEventAllEvents]; 

[yourButton addTarget:self 
               action:@selector(yourAction:)
     forControlEvents:UIControlEventTouchUpInside];
○闲身 2025-01-01 18:53:32

您可以使用相同的操作目标,而不是使用两个目标。在一个目标中,您必须像下面这样区分

-(void)btnAction
{
        if(target1)
          {
            // code for target 1
          }

        else
         {
          // code for target 2
         }

}

这里 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

-(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.

I hope this will helps You.

ぇ气 2025-01-01 18:53:32

我最近制作了一个应用程序,也遇到了同样的情况,但我找到了另一种方法来解决它,所以我决定与可能处于相同情况的人分享我的解决方案。

我将尝试解释我在这个问题的上下文中做了什么:

  • 我向 button 添加了一个标签,并将其与 button 的功能之一关联起来> 需要调用(例如 aMethodShow:)。

  • button 始终调用相同的函数(例如 callSpecificFunc:)。 callSpecificFunc: 的作用是根据当前 button 标签调用函数 aMethodShow:aMethodHide

  • 按钮需要调用不同函数的特定部分中,我仅更改按钮的标签。

像这样的东西:

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
    }
}

当然它可以应用于两个以上的功能:)

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 that button needs to call (aMethodShow: for example).

  • button always call the same function (callSpecificFunc: for example). What callSpecificFunc: does is call either function aMethodShow: or aMethodHide according with the current button tag.

  • In the particular section in which the button needs to call a different function, i only change the tag of 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 :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文