7

这是我以编程方式创建按钮的代码:

NSArray *buttonImage=[NSArray arrayWithObjects:[UIImage imageNamed:@"Cover_0.png"],
                      [UIImage imageNamed:@"Cover_1.png"],
                      [UIImage imageNamed:@"Cover_2.png"],
                      [UIImage imageNamed:@"Cover_3.png"],
                      [UIImage imageNamed:@"Cover_4.png"],
                      [UIImage imageNamed:@"Cover_5.png"],
                      [UIImage imageNamed:@"Cover_6.png"],
                      [UIImage imageNamed:@"Cover_7.png"],nil];

UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(0, 0, 200.0f, 200.0f);
[button setTitle:[NSString stringWithFormat:@"%i", index] forState:UIControlStateNormal];
[button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
button.titleLabel.font = [button.titleLabel.font fontWithSize:50];

[button setImage:buttonImage forState:UIControlStateNormal];
[button addTarget:self action:@selector(buttonTapped:) forControlEvents:UIControlEventTouchUpInside];
return button;

但是在运行它时,我无法进入模拟器并且它会抛出 Signal Sigabart 错误。任何人都可以帮忙吗?

4

5 回答 5

7

要使用选择器数组以编程方式创建按钮,您可以使用以下代码片段:

//定义策略

@interface DataItemStrategy : NSObject
@property(nonatomic, assign) SEL someSelector;
@end

@implementation DataItemStrategy

@synthesize someSelector = _someSelector;

-(id)initWithSelector:(SEL)someSelector
{
    self = [super init];
    if (self)
    {
        self.someSelector = someSelector;
    }
    return self;
}

@end


-(void)createButtons
{
    NSArray *buttonImages =[NSArray arrayWithObjects:[UIImage imageNamed:@"Cover_0.png"],
                          [UIImage imageNamed:@"Cover_1.png"],
                          [UIImage imageNamed:@"Cover_2.png"],nil];

    NSArray *dataStrategies = [NSArray arrayWithObjects:
                                   [[[DataItemStrategy alloc] initWithSelector:@selector(buttonAction0:)] autorelease],
                                   [[[DataItemStrategy alloc] initWithSelector:@selector(buttonAction1:)] autorelease],
                                   [[[DataItemStrategy alloc] initWithSelector:@selector(buttonAction2:)] autorelease], nil];

    CGRect buttonFrame = CGRectMake(0, 0, 50, 50);
    for (NSInteger i = 0; i < [buttonImages count]; i++)
    {
        buttonFrame.origin.y += 50;
        UIImage *buttonImage = [buttonImages objectAtIndex:i];
        DataItemStrategy *dataStrategie = [dataStrategies objectAtIndex:i];
        NSString *title = [NSString stringWithFormat:@"%d", i];
        UIButton *button = [self buttonWithFrame:buttonFrame image:buttonImage action:dataStrategie.someSelector title:title];
        [self.view addSubview:button];
    }    
}

-(UIButton *)buttonWithFrame:(CGRect)buttonFrame image:(UIImage *)buttonImage action:(SEL)buttonAction title:(NSString *)title
{
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = buttonFrame;
    [button setTitle:title forState:UIControlStateNormal];
    [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    button.titleLabel.font = [button.titleLabel.font fontWithSize:50];

    [button setImage:buttonImage forState:UIControlStateNormal];
    [button addTarget:self action:buttonAction forControlEvents:UIControlEventTouchUpInside];
    return button;
}
于 2012-06-16T11:48:40.263 回答
2

您不能将图像数组设置为UIButton图像属性。如果您查看UIButton的文档,您会发现它需要一个UIImage.

于 2012-06-16T06:35:29.000 回答
2

您的代码中的索引是什么?设置这个:

[button setBackgroundImage:[buttonImage objectAtIndex:index] forState:UIControlStateNormal];
于 2012-06-16T06:55:11.803 回答
1

修改如下代码以将第一个图像设置为按钮/您可以使用 for 循环创建图像并将其设置为 uibuttons

NSArray *buttonImage=[NSArray arrayWithObjects:[UIImage imageNamed:@"Cover_0.png"],
                  [UIImage imageNamed:@"Cover_1.png"],
                  [UIImage imageNamed:@"Cover_2.png"],
                  [UIImage imageNamed:@"Cover_3.png"],
                  [UIImage imageNamed:@"Cover_4.png"],
                  [UIImage imageNamed:@"Cover_5.png"],
                  [UIImage imageNamed:@"Cover_6.png"],
                  [UIImage imageNamed:@"Cover_7.png"],nil];

UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(0, 0, 200.0f, 200.0f);
[button setTitle:[NSString stringWithFormat:@"%i", index] forState:UIControlStateNormal];
[button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
button.titleLabel.font = [button.titleLabel.font fontWithSize:50];

[button setImage:(UIImage*)[buttonImage objectAtIndex:1] forState:UIControlStateNormal];
[button addTarget:self action:@selector(buttonTapped:) forControlEvents:UIControlEventTouchUpInside];
return button;

换线

[button setImage:(UIImage*)[buttonImage objectAtIndex:1] forState:UIControlStateNormal];

这会将图像设置为按钮。您已发送一个数组来设置图像,因此应用程序将崩溃

于 2012-06-16T06:33:15.497 回答
1

试试:[button setImage:[buttonImage objectAtIndex:index] forState:UIControlStateNormal];

于 2012-06-16T06:37:54.713 回答