UIButton动作不是从我自己的班级开始的

时间:2013-10-01 09:03:26

标签: ios objective-c

我有一个动态生成UIButtons的类,我希望将选择器操作保持在与方法相同的类中,以使其成为通用的。当我点击按钮时它会崩溃。贝娄是我的代码

RB_RadioButton.h

#import <Foundation/Foundation.h>

@interface RB_RadioButton : NSObject {
    NSMutableArray *options;
}

-(id)initWithOptions:(NSArray *)options;

-(void)renderRadioButtons:(UIView *)view initialXPos:(int)initialXPos initialYPos:(int)initialYPos height:(int)height width:(int)width spacing:(int)spacing;

@end

RB_RadioButton.m

#import "RB_RadioButton.h"

@implementation RB_RadioButton {
    NSMutableArray *buttonArray;
}

-(id)initWithOptions:(NSArray *)optionsArray {
    if(self = [super init]){
        options = [[NSMutableArray alloc]initWithArray:optionsArray];
    }
    return self;
}

-(void)renderRadioButtons:(UIView *)view initialXPos:(int)initialXPos initialYPos:(int)initialYPos height:(int)height width:(int)width spacing:(int)spacing {
    buttonArray = [[NSMutableArray alloc]init];
    int xpos = initialXPos, ypos = initialYPos;
    for (int i = 0; i < options.count; i++) {
        UIButton *radio = [[UIButton alloc]initWithFrame:CGRectMake(xpos, ypos, height, width)];
        radio.backgroundColor = [UIColor grayColor];
        [radio setTag:i];

        [radio addTarget:[RB_RadioButton class] action:@selector(actionTap) forControlEvents:UIControlEventTouchUpInside];
        UILabel *l = [[UILabel alloc]initWithFrame:CGRectMake(xpos+30, ypos, height, width)];
        l.text = [options objectAtIndex:i];
        ypos = ypos + height + spacing;       
        [view addSubview:l];
        [view addSubview:radio];
    }
}
-(void)actionTap{
    NSLog(@"lll");
}

@end

viewController.m

#import "RB_ViewController.h"
#import "RB_RadioButton.h"

@interface RB_ViewController ()

@end

@implementation RB_ViewController

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

    NSArray *arr = [[NSArray alloc]initWithObjects:@"a",@"b",@"c", nil];

    RB_RadioButton *rd = [[RB_RadioButton alloc]initWithOptions:arr];

    [rd renderRadioButtons:self.view initialXPos:20 initialYPos:20 height:20 width:20 spacing:10];

}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

上面的代码在调试控制台中没有任何消息就崩溃了。

请帮忙!

由于

2 个答案:

答案 0 :(得分:0)

那是什么:

[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];
}

答案 1 :(得分:0)

每当你编写这行代码时,我都会采用类方法,因此将actionTap定义为类方法,然后它就像下面一样工作。

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

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