如何为UIButton创建BOOL属性以进行选中

时间:2014-05-18 08:32:45

标签: ios objective-c properties uibutton

我创建了许多带有数组循环的按钮,但我想点击任何按钮时我可以选中状态这个按钮带有一个bool属性,名称为:已选择

当我点击它的任何按钮选择将背景改为红色时,取消选择时将背景改为蓝色。

请指导我如何为任何按钮创建一个属性。

这是我的代码:

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    NSMutableArray *arrayString = [[NSMutableArray alloc]initWithObjects:@"Mohammad",@"Hamidreza",@"Ahmad",@"Amirhossein",@"javad",@"asdasddasdsdasd", nil];

    UIFont *myFont = [UIFont fontWithName:@"Helvetica" size:20];
    CGFloat x = 5;
    CGFloat y = 80;
    for (NSString *text in arrayString) {
        UIButton *button = [[UIButton alloc]init];
        CGFloat width = [self widthOfString:text withFont:myFont];
        CGFloat height = [self heightOfString:text withFont:myFont];
        if ((x + width) > 320) {
            NSLog(@"after line");
            x = 5;
            y += (height+20)+10;
            button.frame = CGRectMake(x, y, width+20, height+20);
            [button setTitle:text forState:UIControlStateNormal];
            button.backgroundColor = [UIColor redColor];
            [self.view addSubview:button];
            x += (width+20)+10;

        }
        else {
            NSLog(@"this line");
            button.frame = CGRectMake(x, y, width+20, height+20);
            [button setTitle:text forState:UIControlStateNormal];
            button.backgroundColor = [UIColor redColor];
            [self.view addSubview:button];
            x += (width+20)+10;
        }
        [button addTarget:self action:@selector(Selected:) forControlEvents:UIControlEventTouchUpInside];

    }
}
-(IBAction)Selected:(id)sender{

    if (//check sender button property bool selected) {

    }
    else {

    }
}

3 个答案:

答案 0 :(得分:1)

试试这段代码: -

- (void)viewDidLoad
{
  [super viewDidLoad];

  [self.button setBackgroundImage:[self imageWithColor:[UIColor redColor]]forState:(UIControlStateHighlighted)];
  [self.button setBackgroundImage:[self imageWithColor:[UIColor redColor]]forState:(UIControlStateSelected)];
  [self.button setBackgroundImage:[self imageWithColor:[UIColor blueColor]] forState:(UIControlStateNormal)];
}

- (UIImage *)imageWithColor:(UIColor *)color {
  CGRect rect = CGRectMake(0.0f, 0.0f, 1.0f, 1.0f);
  UIGraphicsBeginImageContext(rect.size);
  CGContextRef context = UIGraphicsGetCurrentContext();

  CGContextSetFillColorWithColor(context, [color CGColor]);
  CGContextFillRect(context, rect);

  UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
  UIGraphicsEndImageContext();

  return image;
}

答案 1 :(得分:1)

请以小写字母命名您的方法。这与你的问题无关,但这是惯例 因为你的问题更多的是关于“如何获得被触摸的按钮?”,而不是实际做的事情:

-(IBAction)selected:(id)sender //may rename this method to, it's quite strange, look like a "state/getter"
{
    UIButton *button = (UIButton *)sender;
    //Do something
    if ([[button backgroundColor] isEqual:[selectedColor]])
    {
        //Do something
    }
    else
    {
        //Do something
    }
}

答案 2 :(得分:0)

我的家伙试试这个: 您不需要为此UIButton创建属性。您可以使用以下代码执行此操作:

-(IBAction)Selected:(id)sender{
    UIButton *button = (UIButton *)sender;
    if (!button.selected) {
        button.backgroundColor = [UIColor blueColor];
        button.selected = YES;
    }
    else{
        button.backgroundColor = [UIColor redColor];
        button.selected = NO;
    }
} 

此代码适用于您创建的任何按钮! :d