Creating Buttons by looping through an array

一个人想着一个人 提交于 2019-12-12 04:37:06

问题


So I asked this question earlier, and got a few responses but things got really messy so I decided to re ask it here.

I'm initializing a scrollview here:

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];

I have arrays for 44 buttons to be created, the arrays contain info about the names, the filenames for the pictures, the coordinates, etc.

I am running this:

for (int i = 0; i < 44; i++) {
    [self makeLabelsAndButtons:i];
    xPresCoord += 10;
}

In my viewDidLoad

And this is the method 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);

}

The last NSLOG shows that the subviews (labels and buttons) are not being added, but I have no idea why.

Button and Label are defined as:

UIButton *Button;
UILabel *Label;

In my .h file

Any help is appreciated!

EDIT: Here is the link to the other question

Creating buttons with an Array


回答1:


Here's my code,

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

}


来源:https://stackoverflow.com/questions/16137323/creating-buttons-by-looping-through-an-array

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!