使用数组创建按钮 [英] Creating buttons with an Array
问题描述
我有 44 个按钮要添加到滚动视图中,并且我试图使用一系列数组来执行此操作.我有用于文件名的名称字符串、图片的 Y 尺寸(所有 x 都相同)和其他一些的数组.
I have 44 buttons that I am adding to a scrollview and I was trying to use a series of arrays to do so. I have arrays for the name strings of the filenames, for the Y sizes of the pics (all x's are the same) and a few others.
我在 viewDidLoad 中运行该方法的代码是这样的:
The code that I have in my viewDidLoad that runs the method is this:
for (int i = 0; i < 44; i++) {
[self makeLabelsAndButtons:i];
}
makeLabelsAndButtons 方法在这里:
And the method makeLabelsAndButtons is here:
-(void)makeLabelsAndButtons:(NSInteger *)indexPath{
NSString *nameString = [nameArray objectAtIndex:indexPath];
NSString *picString = [picArray objectAtIndex:indexPath];
NSInteger picNumb = [[numbers objectAtIndex:indexPath] integerValue];
NSInteger picXnum = [[picXCoords objectAtIndex:indexPath] integerValue];
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];
[ScrollView addSubview:Button];
[ScrollView insertSubview:Button atIndex:1];
NSLog(@"name: %@, pic:%@, picY:%i", nameString, picString, picNumb);
}
没有添加按钮,但我没有收到任何错误.我真的不确定我做错了什么
The buttons are not being added, but I get no errors. I'm really not sure what I'm doing wrong
感谢您的帮助!
这就是我初始化滚动视图的方式
This is how I'm initializing my scrollview
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];
[self.view insertSubview:ScrollView atIndex:1];
ScrollView.contentSize = CGSizeMake(20000,480);
通过 NSLogs 系统,我确定按钮没有添加到视图中,我不知道为什么.
Via a system of NSLogs I've determined that the buttons aren't being added to the view and I don't know why.
推荐答案
看起来你的 scrollView 是水平滚动的.
It seems like your scrollView is horizontally scrollable.
你可以试试
-(void)addButtonsToScrollView
{
//X Offset for button seperation
CGFloat xOffset = 10.0f;
NSUInteger buttonCount = 0;
//In each iteration buttonFrame would be adjusted to next buttonFrame
//This would be used to find the contentSize of scrollView
CGRect buttonFrame = CGRectMake(xOffset, 240.0, 220.0, 40.0f);
while (buttonCount<44)
{
NSString *nameString = nameArray[buttonCount];
NSString *picString = picArray[buttonCount];
CGFloat picNumb = [numbers[buttonCount] floatValue];
CGFloat picXnum = [picXCoords[buttonCount] floatValue];
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button addTarget:self
action:@selector(presPressed:)
forControlEvents:UIControlEventTouchUpInside];
buttonFrame.origin.y = (scrollView.frame.size.height - picNumb)/2;
buttonFrame.size.height = picNumb;
button.frame = buttonFrame;
[button setBackgroundImage:[UIImage imageNamed:picString]
forState: UIControlStateNormal];
[button setTitle:nameString
forState:UIControlStateNormal];
[scrollView addSubview:button];
buttonFrame.origin.x += buttonFrame.size.width+xOffset;
buttonCount++;
}
CGSize contentSize = scrollView.frame.size;
contentSize.width = buttonFrame.origin.x;
scrollView.contentSize = contentSize;
}
这篇关于使用数组创建按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!