首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将一系列按钮制作成一个数组ios

将一系列按钮制作成一个数组ios
EN

Stack Overflow用户
提问于 2013-05-03 23:52:57
回答 3查看 2.8K关注 0票数 0

我有大约10-12个按钮,我要添加到我的滚动视图。我怎样才能把它们变成一个按钮数组,从而简化代码呢?到目前为止,我的代码(只显示了前三个按钮)如下:

代码语言:javascript
复制
    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];

..。

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2013-05-04 00:16:13

你能看看这是不是可能的

代码语言:javascript
复制
- (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;
}
票数 2
EN

Stack Overflow用户

发布于 2013-05-04 00:00:03

您可以使用:

代码语言:javascript
复制
[NSArray arrayWithObjects:redButton,greenButton,blueButton,nil];

但是使用NSDictionary可能更好。

代码语言:javascript
复制
[NSDictionary dictionaryWithObjectsAndKeys:
                redButton, @"red",
                blueButton, @"blue",
                greenButton, @"green",
                nil];

这样,您就可以使用键而不是索引来查找它们。

票数 0
EN

Stack Overflow用户

发布于 2013-05-04 00:15:54

好的,我们有了解决方案,试试下面的代码,

代码语言:javascript
复制
-(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++;
    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/16362972

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档