2013-04-21 81 views
0

所以我之前问过这个问题,并得到了一些答复,但事情变得非常混乱,所以我决定在这里重新提问。通过在数组中循环创建按钮

我在这里初始化滚动视图:

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

+0

我想你的代码的一天,这对我的工作确定。 ScrollView不是零吗?你怎么声明Button?他们是属性还是ivars? – rdelmar 2013-04-21 23:38:06

+0

ScrollView不是零,ScrollView既是一个属性,也是我在头文件中声明的UIScrollView,我知道它可行,因为我在界面生成器中有一些项目,它们在那里,所以我知道视图存在。按钮定义为UIButton * Button;在我的标题 – tyler53 2013-04-21 23:42:34

+0

中可以看到您使用的代码的.h和.m文件吗?看看有什么不同? – tyler53 2013-04-21 23:43:14

回答

0

这里是我的代码,

#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); 

} 
+0

由于某种原因而不适用于我:/ – tyler53 2013-04-22 00:07:05

+0

您是否开始一个新项目并将此代码放入? – rdelmar 2013-04-22 00:08:56

+0

好吧,当我把它放到一个新的项目中,但我不明白为什么它不在我现有的 – tyler53 2013-04-22 00:11:44