0

我有一个动态生成 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

视图控制器.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

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

请帮忙 !

谢谢

4

2 回答 2

0

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

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

+(void)actionTap{
    NSLog(@"lll");
}
于 2013-10-01T09:18:30.303 回答
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];
}
于 2013-10-01T09:11:56.183 回答