0

我有 44 个按钮要添加到滚动视图中,并且我试图使用一系列数组来执行此操作。我有用于文件名的名称字符串的数组,用于图片的 Y 大小(所有 x 都相同)和其他一些。

我在 viewDidLoad 中运行该方法的代码是这样的:

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

makeLabelsAndButtons 方法在这里:

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

}

没有添加按钮,但我没有收到任何错误。我真的不确定我做错了什么

谢谢你的帮助!

编辑:这就是我初始化滚动视图的方式

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

通过 NSLogs 系统,我确定按钮没有被添加到视图中,我不知道为什么。

4

3 回答 3

0

看起来你的 scrollView 是水平滚动的。

你可以试试

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

}
于 2013-04-20T20:50:07.837 回答
0

根据以下更改您的代码.....它可能会工作

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

将上面替换为

NSString *path = [[NSBundle mainBundle] pathForResource:@"picString" ofType:@"png"];
[Button setBackgroundImage:[[UIImage alloc] initWithContentsOfFile:path] forState: UIControlStateNormal];
于 2013-04-20T20:22:41.877 回答
0

我假设您有两个属性ButtonScrollView在您的控制器上声明。

  • ScrollView为了scrollView便于阅读,将该属性重命名为。

    @property (nonatomic, weak) IBOutlet UIScrollView *scrollView;
    
  • 删除该属性Button,因为您不需要它。

  • 更新您的代码:

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

您不需要同时添加子视图并插入它。

可能是您没有ScrollView向 nil 对象分配和发送消息不会给您错误。您需要确认 self.scrollView 不为零,如果是,则检查它是否在您的视图中正确连接。

于 2013-04-20T20:36:29.187 回答