ios – 如何通过编程方式为UIButtons组设置单个背景图像

前端之家收集整理的这篇文章主要介绍了ios – 如何通过编程方式为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,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错误.
有人可以帮忙吗?

解决方法

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

//定义策略

@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"],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,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;
}
原文链接:https://www.f2er.com/iOS/332304.html

猜你在找的iOS相关文章