我有 44 个按钮要添加到滚动视图中,并且我试图使用一系列数组来执行此操作。我有用于文件名的名称字符串的数组,用于图片的 Y 大小(所有 x 都相同)和其他一些。
我在 viewDidLoad 中运行该方法的代码是这样的:
for (int i = 0; i < 44; i++) {
[self makeLabelsAndButtons:i];
}
makeLabelsAndButtons 方法在这里:
-(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);
}
没有添加按钮,但我没有收到任何错误。我真的不确定我做错了什么
谢谢你的帮助!
编辑:这就是我初始化滚动视图的方式
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 系统,我确定按钮没有被添加到视图中,我不知道为什么。