通过遍历数组创建按钮 [英] Creating Buttons by looping through an array
本文介绍了通过遍历数组创建按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
所以我早些时候问了这个问题,得到了一些答复,但是事情真的很混乱,所以我决定在这里再问。
So I asked this question earlier, and got a few responses but things got really messy so I decided to re ask it here.
我正在初始化滚动视图此处:
I'm initializing a scrollview here:
ScrollView=[[UIScrollView alloc]initWithFrame:CGRectMake(0,0,320,480)];
ScrollView.showsVerticalScrollIndicator=YES;
ScrollView.scrollEnabled=YES;
ScrollView.bounces = YES;
ScrollView.userInteractionEnabled=YES;
[self.view addSubview:ScrollView];
ScrollView.contentSize = CGSizeMake(10500,480);
[self.view addSubview:homeButton];
我有用于创建44个按钮的数组,该数组包含有关名称,文件名的信息图片,坐标等。
I have arrays for 44 buttons to be created, the arrays contain info about the names, the filenames for the pictures, the coordinates, etc.
我正在运行:
for (int i = 0; i < 44; i++) {
[self makeLabelsAndButtons:i];
xPresCoord += 10;
}
在我看来,DidLoad
In my viewDidLoad
这是方法makeLabelsAndButtons:
And this is the method makeLabelsAndButtons:
-(void)makeLabelsAndButtons:(NSInteger)indexPath{
Button = [UIButton buttonWithType:UIButtonTypeCustom];
[Button addTarget:self action:@selector(presPressed:) forControlEvents:UIControlEventTouchUpInside];
Button.frame = CGRectMake(160.0, 240.0, 220.0, [[numbers objectAtIndex:indexPath] integerValue]);
Button.center = CGPointMake(xPresCoord, 200.0);
[Button setBackgroundImage:[UIImage imageNamed:[picArray objectAtIndex:indexPath]] forState: UIControlStateNormal];
[self.ScrollView addSubview:Button];
Label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 0, 32)];
Label.text = [nameArray objectAtIndex:indexPath];
Label.center = CGPointMake([[numbers objectAtIndex:indexPath] integerValue], 390);
[Label setFont:[UIFont fontWithName:@"GurmukhiMN" size:27]];
Label.numberOfLines = 1;
Label.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.5f];
[Label sizeToFit];
Label.textColor= [UIColor whiteColor];
[ScrollView addSubview:Label];
//[ScrollView insertSubview:Label atIndex:1];
//NSLog(@"name: %@, pic:%@, picY:%i", nameString, picString, picNumb);
for (UIView *subview in ScrollView.subviews) NSLog(@"View: %@", subview);
}
最后一个NSLOG显示子视图(标签和按钮)
The last NSLOG shows that the subviews (labels and buttons) are not being added, but I have no idea why.
按钮和标签的定义为:
UIButton *Button;
UILabel *Label;
在我的.h文件中
任何
编辑:这是另一个问题的链接
Here is the link to the other question
推荐答案
这是我的代码,
#import "ViewController.h"
@interface ViewController ()
@property (strong,nonatomic) UIScrollView *scrollView;
@property (strong,nonatomic) NSArray *nameArray;
@property (strong,nonatomic) NSArray *numbers;
@property (strong,nonatomic) NSArray *picXCoords;
@property (strong,nonatomic) NSArray *picArray;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.nameArray = @[@"First",@"Second",@"Third"];
self.numbers = @[@30,@50,@70];
self.picXCoords = @[@160,@400,@700];
self.picArray = @[@"back1.tiff", @"back2.tiff",@"Baldacci.tiff"];
self.scrollView=[[UIScrollView alloc]initWithFrame:CGRectMake(0,0,320,480)];
self.scrollView.showsVerticalScrollIndicator=YES;
self.scrollView.scrollEnabled=YES;
self.scrollView.bounces = YES;
self.scrollView.userInteractionEnabled=YES;
[self.view addSubview:self.scrollView];
self.scrollView.contentSize = CGSizeMake(20000,480);
for (int i = 0; i < 3; i++) {
[self makeLabelsAndButtons:i];
}
}
-(void)makeLabelsAndButtons:(NSInteger)indexPath{
NSString *nameString = [self.nameArray objectAtIndex:indexPath];
NSString *picString = [self.picArray objectAtIndex:indexPath];
NSInteger picNumb = [[self.numbers objectAtIndex:indexPath] integerValue];
NSInteger picXnum = [[self.picXCoords objectAtIndex:indexPath] integerValue];
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button addTarget:self action:@selector(presPressed:) forControlEvents:UIControlEventTouchUpInside];
button.frame = CGRectMake(160.0, 240.0, 220.0, picNumb);
button.center = CGPointMake(picXnum, 200.0);
[button setBackgroundImage:[UIImage imageNamed:picString] forState: UIControlStateNormal];
[button setTitle:self.nameArray[indexPath] forState:UIControlStateNormal];
[self.scrollView addSubview:button];
NSLog(@"name: %@, picY:%i", nameString, picNumb);
}
这篇关于通过遍历数组创建按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文