2013-04-22 27 views
0

您好我想添加多個UIButtons編程。其實我有NSMutableArray。我想創造NSMutableArray許多許多UIButtons儘可能多的元素。我也希望在UIView的每一行中必須有3個UIButton。創建UIButton不是男人的問題問題我寫算法,用於動態創建buttons。這是代碼提前如何添加多發uibuttons編程

-(IBAction)seeAll:(id)sender 
{ 
    if ([barBtn.title isEqualToString:@"See All"]) 
    { 


     containerVw = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 320, 480)]; 
     [containerVw setBackgroundColor:[UIColor whiteColor]]; 
     [srcVw removeFromSuperview]; 
     [self.view addSubview:containerVw]; 

     NSUInteger i; 
     int xCoord; 
     int yCoord=20; 
     int buttonWidth=80; 
     int buttonHeight=50; 
     int buffer = 10; 
     for (i = 1; i <= imageArray.count; i++) 
     { 

      UIButton *aButton = [UIButton buttonWithType:UIButtonTypeCustom]; 
      aButton.frame = CGRectMake(xCoord, yCoord,buttonWidth,buttonHeight); 
      [aButton setBackgroundImage:[imageArray objectAtIndex:i-1] forState:UIControlStateNormal]; 
      [aButton addTarget:self action:@selector(whatever:) forControlEvents:UIControlEventTouchUpInside]; 
      [containerVw addSubview:aButton]; 



      if (i%4 == 0) 
      { 
       yCoord += buttonHeight + buffer; 

      } 
      NSLog(@"xcoordinate %i",xCoord); 
      NSLog(@"ycoordinate %i",yCoord); 

     } 
+0

你在哪裏設置你的按鈕你檢查 – Rajneesh071 2013-04-22 09:28:49

+0

我的代碼的XCOORD? – Rajneesh071 2013-04-23 07:45:05

回答

1
- (void)createButtonGrid 
{ 
    int x, y, width, height, exceedSpace; 
    exceedSpace = 6; 
    x = y = exceedSpace; 
    width = height = 100; 

    UIButton *btn; 

    for (int i=0; i< 10; i++) 
    { 
     btn = [UIButton buttonWithType:UIButtonTypeCustom]; 
     [btn setFrame:CGRectMake(x, y, width/2, height/2)]; 
     [btn setTag:i]; 
     [btn addTarget:self action:@selector(yourMethod:) forControlEvents:UIControlEventTouchUpInside]; 

     [self.scrollview addSubview:btn]; 

     if((i+1) % 3 == 0) 
     { 
      x = exceedSpace; 
      y += (width/2)+exceedSpace; 
     } 
     else 
      x += (width/2)+exceedSpace; 
    } 
} 
+0

好,謝謝它的工作對我來說非常感謝 – zeshan 2013-04-22 09:48:16

+0

歡迎您@zeshan – 2013-04-22 09:49:40

+0

你知道你的密碼是比其他人更靈活,因爲在你的代碼中,我只需要改變循環的值,並自動創建許多按鈕 – zeshan 2013-04-22 09:53:03

1

親切更正此感謝看到我的波紋管的答案,並得到一些想法..

 int imageIndex = 0; 

     int yOffset = 4; 

     while (imageIndex < imageArray.count)   
     { 
      int yPos = 7 + yOffset * 30; 

      for(int i = 0; i < 4; ++i)//set 4 for 4 columns 
      { 
       CGRect rect = CGRectMake((0 + i * 80), yPos, 80, 31); 

       if (imageIndex < [imageArray count]) { 

        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom]; 
        button.frame = rect; 
        button.tag = imageIndex; 
        [button addTarget:self 
           action:@selector(btnTemp_Clicked:) 
        forControlEvents:UIControlEventTouchDown]; 

        [button setTitle:[imageArray objectAtIndex:imageIndex] forState:UIControlStateNormal]; 
    //     [button.titleLabel setFont:[UIFont fontWithName:@"Helvetica-Bold" size:12]]; 
        [button setBackgroundImage:[imageArray objectAtIndex:imageIndex] forState:UIControlStateNormal]; 

        [button setTitleColor:[UIColor redColor] forState:UIControlStateHighlighted ]; 

        [button setNeedsDisplay]; 

        [containerVw addSubview:button]; 
       } 
       ++imageIndex; 
      } 
      ++yOffset; 
     } 
+0

@zeshan我在你GER原糖與陣列上面的回答數/ 4和列是4每生... :) – 2013-04-22 09:35:23

0

你在哪裏設置你的按鈕的XCOORD

for (i = 1; i <= imageArray.count; i++) 
    { 
     if (i%4 == 0) 
     { 
      yCoord += buttonHeight + buffer; 
      xCoord=0; 
     } 
     else{ 
      xCoord += buttonWidht + buffer; 
     } 
     NSLog(@"xcoordinate %i",xCoord); 
     NSLog(@"ycoordinate %i",yCoord); 

     UIButton *aButton = [UIButton buttonWithType:UIButtonTypeCustom]; 
     aButton.frame = CGRectMake(xCoord, yCoord,buttonWidth,buttonHeight); 
     [aButton setBackgroundImage:[imageArray objectAtIndex:i-1] forState:UIControlStateNormal]; 
     [aButton addTarget:self action:@selector(whatever:) forControlEvents:UIControlEventTouchUpInside]; 
     [containerVw addSubview:aButton]; 
    } 
相關問題