所以我之前问过这个问题,并得到了一些答复,但事情变得非常混乱,所以我决定在这里重新提问。通过在数组中循环创建按钮
我在这里初始化滚动视图:
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;
}
在我的viewDidLoad
这是方法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显示未添加子视图(标签和按钮),但我不知道为什么。
UIButton *Button;
UILabel *Label;
在我的.h文件
任何帮助表示赞赏:为
按钮和标签的定义!
编辑:这里是链接到其他问题
Creating buttons with an Array
我想你的代码的一天,这对我的工作确定。 ScrollView不是零吗?你怎么声明Button?他们是属性还是ivars? – rdelmar 2013-04-21 23:38:06
ScrollView不是零,ScrollView既是一个属性,也是我在头文件中声明的UIScrollView,我知道它可行,因为我在界面生成器中有一些项目,它们在那里,所以我知道视图存在。按钮定义为UIButton * Button;在我的标题 – tyler53 2013-04-21 23:42:34
中可以看到您使用的代码的.h和.m文件吗?看看有什么不同? – tyler53 2013-04-21 23:43:14