Ios UIButton操作不是从我自己的类中触发的

Ios UIButton操作不是从我自己的类中触发的,ios,objective-c,Ios,Objective C,我有一个动态生成UIButtons的类,我希望将选择器操作保持在与方法相同的类中,使其成为泛型。当我点击按钮时,它就崩溃了。下面是我的密码 RB_单选按钮.h 上面的代码在调试控制台中没有任何消息就崩溃了 请帮忙 谢谢你那是什么 [radio addTarget:[RB_RadioButton class] action:@selector(actionTap) forControlEvents:UIControlEventTouchUpInside]; actionTap是一种正常的方法,它

我有一个动态生成UIButtons的类,我希望将选择器操作保持在与方法相同的类中,使其成为泛型。当我点击按钮时,它就崩溃了。下面是我的密码

RB_单选按钮.h 上面的代码在调试控制台中没有任何消息就崩溃了

请帮忙

谢谢你那是什么

[radio addTarget:[RB_RadioButton class] action:@selector(actionTap) forControlEvents:UIControlEventTouchUpInside];
actionTap
是一种正常的方法,它需要
self
指针,但您正试图在
Class
对象上调用它

这一行应该如下所示:

[radio addTarget: self action:@selector(actionTap) forControlEvents:UIControlEventTouchUpInside];
要使其工作,您还必须修复内存管理。控制器应记住您的
RB_RadioButton
对象作为一个字段,并以dealloc方法释放它

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    NSArray *arr = [NSArray arrayWithObjects: @"a",@"b",@"c", nil]; // here also you had a memory leak

    rd = [[RB_RadioButton alloc]initWithOptions:arr]; // rd is object field
    [rd renderRadioButtons:self.view initialXPos:20 initialYPos:20 height:20 width:20 spacing:10];
}

- (void)dealloc {
    [rd release];
    [super dealloc];
}

每当您编写这行代码时,它都会使用class方法,因此将
actionTap
定义为class方法,然后它的工作方式如下所示

[radio addTarget:[RB_RadioButton class] action:@selector(actionTap) forControlEvents:UIControlEventTouchUpInside];

+(void)actionTap{
    NSLog(@"lll");
}

像这样尝试[radio addTarget:self action:@selector(actionTap)forControlEvents:UIControlEventTouchUpInside];作为addTarget的“目标”:。。。您应该传递object的方法是否向项目添加了异常断点?如果它崩溃了,那么它应该给你一个崩溃日志。
[radio addTarget: self action:@selector(actionTap) forControlEvents:UIControlEventTouchUpInside];
- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    NSArray *arr = [NSArray arrayWithObjects: @"a",@"b",@"c", nil]; // here also you had a memory leak

    rd = [[RB_RadioButton alloc]initWithOptions:arr]; // rd is object field
    [rd renderRadioButtons:self.view initialXPos:20 initialYPos:20 height:20 width:20 spacing:10];
}

- (void)dealloc {
    [rd release];
    [super dealloc];
}
[radio addTarget:[RB_RadioButton class] action:@selector(actionTap) forControlEvents:UIControlEventTouchUpInside];

+(void)actionTap{
    NSLog(@"lll");
}