Creating buttons with an Array

99封情书 提交于 2020-01-06 12:27:29

问题


I have 44 buttons that I am adding to a scrollview and I was trying to use a series of arrays to do so. I have arrays for the name strings of the filenames, for the Y sizes of the pics (all x's are the same) and a few others.

The code that I have in my viewDidLoad that runs the method is this:

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

And the method makeLabelsAndButtons is here:

-(void)makeLabelsAndButtons:(NSInteger *)indexPath{
NSString *nameString = [nameArray objectAtIndex:indexPath];
NSString *picString = [picArray objectAtIndex:indexPath];
NSInteger picNumb = [[numbers objectAtIndex:indexPath] integerValue];
NSInteger picXnum = [[picXCoords objectAtIndex:indexPath] integerValue];

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];
[ScrollView addSubview:Button];
[ScrollView insertSubview:Button atIndex:1];

NSLog(@"name: %@, pic:%@, picY:%i", nameString, picString, picNumb);

}

The buttons are not being added, but I get no errors. I'm really not sure what I'm doing wrong

Thanks for any help!

EDIT: This is how I'm initializing my 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];
[self.view insertSubview:ScrollView atIndex:1];
ScrollView.contentSize = CGSizeMake(20000,480);

Via a system of NSLogs I've determined that the buttons aren't being added to the view and I don't know why.


回答1:


Change Your code according to following..... it may work

[Button setBackgroundImage:[UIImage imageNamed:picString] forState: UIControlStateNormal];

Replace above with

NSString *path = [[NSBundle mainBundle] pathForResource:@"picString" ofType:@"png"];
[Button setBackgroundImage:[[UIImage alloc] initWithContentsOfFile:path] forState: UIControlStateNormal];



回答2:


I assume that you have two properties Button and ScrollView declared on your controller.

  • Rename the property ScrollView to scrollView for readability.

    @property (nonatomic, weak) IBOutlet UIScrollView *scrollView;
    
  • Delete the property Button because you don't need it.

  • Update your code:

    Button *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];
    [self.scrollView addSubview:button];
    

You don't need to both add the subview and insert it.

It might be that you weren't allocating ScrollView and sending messages to nil objects will not give you an error. You need to confirm that self.scrollView is not nil, if it is, then check that it is wired up properly in your view.




回答3:


It seems like your scrollView is horizontally scrollable.

You can try

-(void)addButtonsToScrollView
{
    //X Offset for button seperation 
    CGFloat xOffset = 10.0f;

    NSUInteger buttonCount = 0;
    //In each iteration buttonFrame would be adjusted to next buttonFrame
    //This would be used to find the contentSize of scrollView
    CGRect buttonFrame = CGRectMake(xOffset, 240.0, 220.0, 40.0f);
    while (buttonCount<44)
    {
        NSString *nameString = nameArray[buttonCount];
        NSString *picString = picArray[buttonCount];

        CGFloat picNumb = [numbers[buttonCount] floatValue];
        CGFloat picXnum = [picXCoords[buttonCount] floatValue];

        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];

        [button addTarget:self
                   action:@selector(presPressed:)
         forControlEvents:UIControlEventTouchUpInside];

        buttonFrame.origin.y = (scrollView.frame.size.height - picNumb)/2;
        buttonFrame.size.height = picNumb;
        button.frame = buttonFrame;

        [button setBackgroundImage:[UIImage imageNamed:picString]
                          forState: UIControlStateNormal];
        [button setTitle:nameString
                forState:UIControlStateNormal];

        [scrollView addSubview:button];

        buttonFrame.origin.x += buttonFrame.size.width+xOffset;
        buttonCount++;

    }

    CGSize contentSize = scrollView.frame.size;
    contentSize.width = buttonFrame.origin.x;
    scrollView.contentSize = contentSize;

}


来源:https://stackoverflow.com/questions/16124584/creating-buttons-with-an-array

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