所以我早些时候问了这个问题,并得到了一些回复,但事情变得非常混乱,所以我决定在这里重新提问。
我在这里初始化一个滚动视图:
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 个按钮的数组,这些数组包含有关名称、图片的文件名、坐标等的信息。
我正在运行这个:
for (int i = 0; i < 44; i++) {
[self makeLabelsAndButtons:i];
xPresCoord += 10;
}
在我看来DidLoad
这是 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 显示没有添加子视图(标签和按钮),但我不知道为什么。
Button 和 Label 定义为:
UIButton *Button;
UILabel *Label;
在我的 .h 文件中
任何帮助表示赞赏!
编辑:这是另一个问题的链接