【问题标题】:IOS callback on UIButton clickUIButton点击的IOS回调
【发布时间】:2014-11-18 09:07:34
【问题描述】:

我有一个在我的应用程序的许多屏幕中使用的按钮。我想用自定义图像和按下按钮时的回调来实现一个自定义按钮。

我习惯了 java,但我不知道如何在 iOS 中解决这个问题。我已经阅读过关于 Category 和 Subclassing 的内容,但我仍然不确定。

有人有例子吗?或者什么是最好的解决方案?非常感谢任何帮助。

我做了什么(感谢@Aris):

.h 文件

#import <UIKit/UIKit.h>

@interface UIButton (MyUIButton)

+ (UIButton *)customButtonWithTarget:(id)target;

@end

.m 文件

#import "UIButton+MyUIButton.h"

@implementation UIButton (MyUIButton)

+ (UIButton *)customButtonWithTarget:(id)target{
    UIButton *button_ = [UIButton buttonWithType:UIButtonTypeSystem];
    [button_ setFrame:CGRectMake(0, 0, 120, 44)];
    [button_ setBackgroundImage:[UIImage imageNamed:@"button_image.png"] forState:UIControlStateNormal];

    [button_ addTarget:target
                action:@selector(event_button_click:)
      forControlEvents:UIControlEventTouchUpInside];

    return button_;
}

@end

在我的视图控制器中:

#import "UIButton+MyUIButton.h"

- (void)viewDidLoad {
    [super viewDidLoad];
UIButton *myButton = [UIButton customButtonWithTarget:self];
    [self.view addSubview:myButton];

}

-(void)event_button_click
{
    // code here
}

我收到此错误:'-[ViewController event_button_click:]: unrecognized selector sent to instance 0x78e5df10'

【问题讨论】:

  • 在xcode中更容易右键单击界面生成器中的按钮并拖动到

标签: ios objective-c callback uibutton custom-controls


【解决方案1】:

子类化 UIButton 是错误的方法。您需要添加一个类别工厂方法,该方法创建一个具有您需要的属性的按钮。 扩展 JNYJ 的答案:

+ (UIButton *)customButtonWithTarget:(id)target{
    UIButton *button_ = [UIButton buttonWithType:UIButtonTypeCustom];
    [button_ setFrame:CGRectMake(0, 0, 120, 44)];
    [button_ setBackgroundImage:[UIImage imageWithContentsOfFile:@"File path"] forState:UIControlStateNormal];

    [button_ addTarget:target 
                action:@selector(event_button_click:) 
      forControlEvents:UIControlEventTouchUpInside];

    return button;
}

您应该将此方法放在可以使用 Xcode 创建的 UIButton 类别中,然后将文件导入到创建按钮所需的所有位置。 导入类别后,您可以像这样调用方法:

[UIButton customButtonWithTarget:target];

您必须确保target 实现了一个名为event_button_click: 的方法。

在典型场景中,Button 的目标应该是负责 View 的 viewController。如果您希望 Button 在所有 ViewControllers 上执行相同的操作,那么 ViewControllers 应该是实现公共操作的 ViewController 的子类。

实现此目的的另一种方法是将目标设置为您知道将在整个应用程序生命周期中存在的对象。候选人可能是Application Delegate 或其他单身人士。

响应 OP 的编辑:

您收到此错误的错误是因为选择器末尾有“:”。 这意味着该方法应采用 1 个参数。 响应按钮点击的典型方法具有以下签名:

- (void)didTapButton:(id)sender

sender 是生成事件的对象,在我们的例子中是按钮。

所以在你的情况下:

-(void)event_button_click:(id)sender
{
    UIButton * myButton = sender
    //custom code
}

【讨论】:

  • 谢谢@Aris,问题是我在哪里添加代理
  • 代表你的意思是你应该使用什么作为目标?
  • 我应该在我的 ViewControllers 中创建 [UIButton customButtonWithTarget:self]; 吗?
  • 非常感谢您的时间和帮助。我犯了一个非常愚蠢的错误..现在它起作用了:)
【解决方案2】:

子类化是一种抽象 UIButton 的方法,这样你就可以在任何你想要的地方使用它来实现相同的功能。

因此,您将从 UIButton 继承。因为 UIButton 继承自 UIControl,所以您无法在 UIButton 的文档中找到添加选择器的方法。您将用于添加选择器的方法在 UIControl 的文档中。使用以下是为按钮添加选择器。

addTarget:action:forControlEvents:

当你想使用它时,你只需要像初始化其他对象一样进行初始化。通常,您将使用“alloc”和“init”。但是 UIButton 有一个工厂来初始化它。

对于多种用途,您可以使用使其成为静态,这样您就不必像创建数据管理器那样每次需要它时都对其进行初始化。

【讨论】:

    【解决方案3】:

    看看这段代码:

    UIButton *button_ = [UIButton buttonWithType:UIButtonTypeCustom];
    [button_ setFrame:CGRectMake(0, 0, 120, 44)];
    [button_ setBackgroundImage:[UIImage imageWithContentsOfFile:@"File path"] forState:UIControlStateNormal];
    [button_ addTarget:self action:@selector(event_button_click:) forControlEvents:UIControlEventTouchUpInside];
    
    [self.view addSubview:button_];
    
    
    
    
    // CALL BACK
    -(void)event_button_click:(id)sender{
    //Do what event you want when click button (Touch up inside)
    }
    

    如果您有任何问题,请告诉我,TKS。

    【讨论】:

    • 感谢您的帮助@JNYJ。问题是我必须在所有 ViewControllers 中重复你的代码。这就是我想做的自定义对象
    【解决方案4】:

    创建自定义按钮类继承自 UIButton 类,并在此处添加按钮所需的自定义项。 在您的按钮class 中定义一个delegate protocol,并带有通知点击事件的方法。

    在你的类中创建按钮并添加为 subview ,在那里实现 delegate 方法以从 button 获取事件。

    【讨论】:

    • 谢谢@Alex,我想这正是我所需要的。你能告诉我如何访问我的按钮子类中的点击事件吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-24
    • 1970-01-01
    • 2014-01-19
    • 1970-01-01
    • 2018-04-22
    • 1970-01-01
    相关资源
    最近更新 更多