创建多个UIButton但只显示最后一个UIButtons

时间:2012-09-10 14:04:57

标签: ios

我在UIView中创建了多个UIButtons。有两个嵌套循环,以创建不同的按钮组,具有不同的形状。问题是,只有最后创建的UIButton是可见的(并且可以与PanGestureRecognizer一起移动),即使按钮应该插入不同的位置。

- (void)viewDidLoad
{
    [super viewDidLoad];
// Do any additional setup after loading the view.

    int i, j;
    NSMutableString *backgoundImageName = [[NSMutableString alloc] init];
    NSMutableString *barTitle = [[NSMutableString alloc] init];
    sticks = [[NSMutableArray alloc] init];

    [mainView setBackgroundColor:[UIColor blackColor]];
    self.view = mainView;
    [mainView setNeedsDisplay];

    //create the bars
    UIButton *barra = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    UIPanGestureRecognizer *panGestureRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePan:)];
    [barra addTarget:self action:@selector(changeButtonTitle:) forControlEvents:UIControlEventTouchUpInside];
    [barra addGestureRecognizer:panGestureRecognizer];
    for (j = 1; j <= 10; j++) {
        backgoundImageName = [NSMutableString stringWithFormat:@"Regolo%d%@",j,@".png"];
        for (i = 1; i <= 3; i++) {
            barra.frame = CGRectMake((SQUARE_SIZE * j), (SQUARE_SIZE * 2), SQUARE_SIZE, (SQUARE_SIZE * j));
            [barra setBackgroundImage:[UIImage imageNamed:backgoundImageName] forState:UIControlStateNormal];
            [barra setTag:j];

            barTitle = [NSMutableString stringWithFormat:@"%d",j];
            [barra setTitle:barTitle forState:UIControlStateNormal];
            [barra setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
            [sticks addObject:barra];
            [self.mainView addSubview:(UIButton *)sticks.lastObject];
        }
    }
}

2 个答案:

答案 0 :(得分:1)

移动此代码:

UIButton *barra = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 
[barra addTarget:self action:@selector(changeButtonTitle:) forControlEvents:UIControlEventTouchUpInside]; 
[barra addGestureRecognizer:panGestureRecognizer]; 

在你的内部for循环中。

你有一个单独的对象(UIButton * barra),它的属性你正在循环中更改,所以我想最后所有的按钮都堆叠在最后一个按钮的框架位置(并且具有最终背景,标题,以及标签值也是),只有最顶层可见。

答案 1 :(得分:0)

你只创建一个按钮。 应该是这样的:

for (j = 1; j <= 10; j++) {
    backgoundImageName = [NSMutableString stringWithFormat:@"Regolo%d%@",j,@".png"];
    for (i = 1; i <= 3; i++) {
        //create the bars
        UIButton *barra = [UIButton buttonWithType:UIButtonTypeRoundedRect];
        UIPanGestureRecognizer *panGestureRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePan:)];
        [barra addTarget:self action:@selector(changeButtonTitle:) forControlEvents:UIControlEventTouchUpInside];
        [barra addGestureRecognizer:panGestureRecognizer];


        barra.frame = CGRectMake((SQUARE_SIZE * j), (SQUARE_SIZE * 2), SQUARE_SIZE, (SQUARE_SIZE * j));
        [barra setBackgroundImage:[UIImage imageNamed:backgoundImageName] forState:UIControlStateNormal];
        [barra setTag:j];

        barTitle = [NSMutableString stringWithFormat:@"%d",j];
        [barra setTitle:barTitle forState:UIControlStateNormal];
        [barra setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [sticks addObject:barra];
        [self.mainView addSubview:barra];
    }
}