IIB回调UIButton点击

时间:2014-11-18 09:07:35

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

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

我已经习惯了java,但我不知道如何在iOS中解决这个问题。我读过类别和子类,但我还不确定。

有没有人有例子?或者最佳解决方案是什么?我真的很感激任何帮助。

我做了什么(感谢@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

在我的ViewController中:

#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'

4 个答案:

答案 0 :(得分: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
}

答案 1 :(得分:0)

子类化是一种抽象UIButton的方法,以便您可以在任何需要的地方使用相同的功能。

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

  

addTarget:动作:forControlEvents:

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

对于多种用法,您可以使用make it static,这样您就不必在每次需要时对其进行初始化,如创建数据管理器。

答案 2 :(得分:0)

看看这些代码:

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。

答案 3 :(得分:0)

创建自定义按钮类继承自UIButton类,并在那里添加按钮所需的自定义项。 在按钮delegate protocol中定义class,其中包含通知点击事件的方法。

在您的班级中创建按钮并添加为subview,在那里实施delegate方法以从button获取事件。