如何以编程方式为一组 UIButtons 设置单独的背景图像

Posted

技术标签:

【中文标题】如何以编程方式为一组 UIButtons 设置单独的背景图像【英文标题】:How to Set individual background image for group of UIButtons by programmatically 【发布时间】:2012-06-16 06:27:42 【问题描述】:

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

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 错误。 有人可以帮忙吗?

【问题讨论】:

【参考方案1】:

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

//定义策略

@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;

【讨论】:

【参考方案2】:

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

[button setBackgroundImage:[buttonImage objectAtIndex:index] forState:UIControlStateNormal];

【讨论】:

【参考方案3】:

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

【讨论】:

【参考方案4】:

修改代码如下,将第一个图像设置为按钮/您可以使用 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];

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

【讨论】:

:tks...但是如果我将 ObjectAtIndex 设置为 1,则只有 Cover_1.png 设置为所有图像,但我需要每个图像,但都有单独的背景图像...你能帮忙吗?... 您需要使用 [buttonImage objectAtIndex:0] 将图像设置为按钮 对于第二个按钮使用 [buttonImage objectAtIndex:1] 第三个 [buttonImage objectAtIndex:2];第四个[bu​​ttonImage objectAtIndex:3];同样你必须设置 :Tks man...我通过将 objectAtIndex 定义为索引来获得输出...但是我尝试通过选择每个按钮来触发不同的操作...这是我的第一个代码按钮 :你能帮我解决如何为每个按钮触发一个动作吗?..是否可以通过将其分组到数组来进行动作?...【参考方案5】:

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

【讨论】:

以上是关于如何以编程方式为一组 UIButtons 设置单独的背景图像的主要内容,如果未能解决你的问题,请参考以下文章

自动居中以编程方式创建的 UIButtons

以编程方式将 UIButtons 添加到 UICollectionView 单元格的 uiimage

以编程方式添加到 UITableViewCell 的 UIButtons 永远不会出现

即使在图像被更改或删除后,以编程方式添加的 UIButtons 也会保留背景图像

根据条件以编程方式拉伸 UIButton 宽度

makefile.am 可以为一组目标设置 LDADD 吗?