简体   繁体   中英

how to create BOOL property for UIButton for checked selecting

I create many button with loop from array but I want when click on any button I could check select state this button with one bool property with name : Selected .

when any button that I click on it selected change background to red color and when deselected change background to blue color.

please guide me how to create one property for any button.

this is my code:

- (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 {

    }
}

Try this code:-

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

Please, name your method starting with a lowercase. That's not related to your issue, but that's convention.
Since your question is more about "How do I get the button that was touched ?", than actually do your thing:

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

my dude try this : you don't need to create property for UIButton for this. you can do it with this code:

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

this code working for any button that you created!!! :D

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM