2012-09-10 93 views
1

我在UIView內創建多個UIButtons。有兩個嵌套循環,以創建不同組的按鈕,具有不同的形狀。問題是,即使按鈕應插入不同位置,只有最後創建的UIButton可見(並且可通過PanGestureRecognizer移動)。創建多個UIButtons,但只有最後一個可見

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

回答

1

移動驗證碼:

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

裏面你內心的for循環。

你有一個單獨的對象(UIButton * barra),它的屬性是在一個循環內改變的,所以我想最後你所有的按鈕都堆放在最後一個按鈕的框架位置(並且最終的背景,標題和標籤值),只有最上面的部分是可見的。

+0

它的工作原理!謝謝! – Samor

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]; 
    } 
} 
+0

iT工作!非常感謝您的新手! – Samor

+0

然後,您應該考慮對答案進行評分並通過單擊複選標記來接受問題。 :) – stk

相關問題