通过循环数组创建按钮

时间:2013-04-21 23:30:57

标签: ios arrays uibutton uilabel

所以我早些时候问了这个问题,得到了一些回复,但事情变得非常混乱所以我决定在这里问一下。

我在这里初始化一个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];
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

1 个答案:

答案 0 :(得分: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);

}