2013-05-03 54 views
0

我有大約10-12個按鈕添加到我的滾動視圖中。我怎樣才能使這些成爲一個按鈕陣列,以便我可以簡化代碼?截至目前我的代碼(僅前三個按鈕顯示)如下:將一系列按鈕組合到一個數組中ios

UIButton *redButton =[UIButton buttonWithType:UIButtonTypeRoundedRect]; 
    redButton.frame = CGRectMake(0, 0, 50, 30); 
    redButton.tag = 2; 
    [redButton setTitle:@"red" forState:UIControlStateNormal]; 
    redButton.backgroundColor = [UIColor redColor]; 
    [redButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]; 
    [redButton addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside]; 
    [self.scollView addSubview:redButton]; 

    UIButton *blueButton =[UIButton buttonWithType:UIButtonTypeRoundedRect]; 
    blueButton.frame = CGRectMake(70, 0, 50, 30); 
    blueButton.tag = 3; 
    [blueButton setTitle:@"blue" forState:UIControlStateNormal]; 
    blueButton.backgroundColor = [UIColor blueColor]; 
    [blueButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]; 
    [blueButton addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside]; 
    [self.scollView addSubview:blueButton]; 

    UIButton *greenButton =[UIButton buttonWithType:UIButtonTypeRoundedRect]; 
    greenButton.frame = CGRectMake(140, 0, 50, 30); 
    greenButton.tag = 5 ; 
    [greenButton setTitle:@"green" forState:UIControlStateNormal]; 
    greenButton.backgroundColor = [UIColor greenColor]; 
    [greenButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]; 
    [greenButton addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside]; 
    [self.scollView addSubview:greenButton]; 

...

+0

你的意思是保存按鈕(這樣你,你看這是可能的是簡化使用它們),還是配置它們(然後真的意味着如何編寫一個循環來配置我的按鈕)? – Wain 2013-05-03 16:03:35

+0

對不起,是的,我的意思是一個循環來配置。 – DaveLass 2013-05-03 16:14:28

回答

2

- (void)addButtonsToScrollView 
{ 
    NSArray *buttons = @[@{@"Tag":@2,@"Title":@"red",@"Color":[UIColor redColor]}, 
         @{@"Tag":@3,@"Title":@"blue",@"Color":[UIColor blueColor]}, 
         @{@"Tag":@5,@"Title":@"green",@"Color":[UIColor greenColor]}]; 

    CGRect frame = CGRectMake(0.0f, 0.0f, 50.0f, 30.0f); 
    for (NSDictionary *dict in buttons) 
    { 
     UIButton *button =[UIButton buttonWithType:UIButtonTypeRoundedRect]; 
     button.frame = frame; 
     button.tag = [dict[@"Tag"] integerValue]; 
     [button setTitle:dict[@"Title"] 
       forState:UIControlStateNormal]; 
     button.backgroundColor = dict[@"Color"]; 
     [button setTitleColor:[UIColor blackColor] 
        forState:UIControlStateNormal]; 
     [button addTarget:self action:@selector(buttonAction:) 
     forControlEvents:UIControlEventTouchUpInside]; 
     [self.scrollView addSubview:button]; 
     frame.origin.x+=frame.size.width+20.0f; 
    } 

    CGSize contentSize = self.scrollView.frame.size; 
    contentSize.width = frame.origin.x; 
    self.scrollView.contentSize = contentSize; 
} 
0

您可以使用:

[NSArray arrayWithObjects:redButton,greenButton,blueButton,nil]; 

但可能會更好用NSDictionary

[NSDictionary dictionaryWithObjectsAndKeys: 
       redButton, @"red", 
       blueButton, @"blue", 
       greenButton, @"green", 
       nil]; 

這樣你可以使用鍵來查找它們而不是索引。

0

好,我們有解決方案,試試這個代碼的隊友,

enter image description here

-(void) createButtons{ 

    NSDictionary *buttonColors = @{@"Red":[UIColor redColor],@"Green":[UIColor greenColor],@"Black":[UIColor blackColor],@"Yellow":[UIColor yellowColor],@"Blue":[UIColor blueColor]}; 

    int tag = 1; 
    for(NSString *key in buttonColors.allKeys){ 
     UIColor *color = [buttonColors objectForKey:key]; 
     UIButton *button =[UIButton buttonWithType:UIButtonTypeRoundedRect]; 
     CGRect frame = CGRectMake(((tag -1)*70), 0, 50, 30); 
     [button setFrame:frame]; 
     button.tag = tag; 
     button.backgroundColor = color; 
     [button setTitleColor:color forState:UIControlStateNormal]; 
     [button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside]; 

     [button setTitle:key forState:UIControlStateNormal]; 
     [self.scrollView addSubview:button]; 

     tag++; 
    } 
} 
相關問題