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

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

概述这是我的一段以编程方式创建按钮的代码: NSArray *buttonImage=[NSArray arrayWithObjects:[UIImage imageNamed:@"Cover_0.png"], [UIImage imageNamed:@"Cover_1.png"], [UIImage imageN 这是我的一段以编程方式创建按钮的代码:
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;}
总结

以上是内存溢出为你收集整理的ios – 如何通过编程方式为UIButtons组设置单个背景图像全部内容,希望文章能够帮你解决ios – 如何通过编程方式为UIButtons组设置单个背景图像所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1101213.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-28
下一篇 2022-05-28

发表评论

登录后才能评论

评论列表(0条)

保存