質問

I have some buttons that get created according to an array

for (NSString *buttonTitle in buttons) {
    self.bottomOptionButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    self.bottomOptionButton.frame = CGRectMake(xPos , buttonHeight , buttonWidth, buttonHeight);
    [self.bottomOptionButton setTitle:buttonTitle forState:UIControlStateNormal];
    [self.bottomOptionButton setTitleColor:[[ThemeManager shared]slipKeyboardFontColor] forState:UIControlStateNormal];
    self.bottomOptionButton.tag = index;
    [self.bottomOptionButton addTarget:self action:@selector(optionBottomButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
    [self.optionsKeyboard addSubview:self.bottomOptionButton];

    xPos += buttonWidth;
    index += 1;

    UIView *lowerVerticalLine = [[UIView alloc]initWithFrame:CGRectMake(xPos, buttonHeight, lineThickness, buttonHeight)];
    lowerVerticalLine.backgroundColor = [[ThemeManager shared]separatorLineColor];
    [self.optionsKeyboard addSubview:lowerVerticalLine];
}

they get created and display fine, now when a button is tapped, i can get what tag is the button referring, to, but i need to set the background color of the buttons by tag which is not working yet

- (void)optionBottomButtonPressed:(id)sender
{
    UIButton *button = sender;

    NSLog(@"tu tag :: %d", button.tag);
    NSLog(@"de opcion :: %@", [self.bottomButtonArray objectAtIndex:button.tag] );

    UIColor *selectedButtonColor = [[ThemeManager shared]buttonSelectedBackground];
    UIColor *nonSelectedButtonColor = [UIColor whiteColor];

    for (int i = 0; i<= self.bottomButtonArray.count; i++) {

        UIButton *button = (UIButton *)[self.bottomOptionButton viewWithTag:i];
        button.backgroundColor = nonSelectedButtonColor;
    }

    [sender setBackgroundColor:[UIColor redColor]];

}

so how to change the background of the buttons by tag?

thanks!

役に立ちましたか?

解決

Try this,

for (NSString *buttonTitle in buttons) {
    UIButton *bottomOptionButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    ....
    bottomOptionButton.tag = index;
    ....
    [self.optionsKeyboard addSubview:bottomOptionButton];

    ....
}

And in optionBottomButtonPressed:

- (void)optionBottomButtonPressed:(id)sender
{
     NSLog(@"tu tag :: %d", button.tag);
     NSLog(@"de opcion :: %@", [self.bottomButtonArray objectAtIndex:button.tag] );

     UIColor *selectedButtonColor = [[ThemeManager shared]buttonSelectedBackground];
     UIColor *nonSelectedButtonColor = [UIColor whiteColor];

     for (int i = 0; i<= self.bottomButtonArray.count; i++) {

         UIButton *button = (UIButton *)[self.optionsKeyboard viewWithTag:i];
         button.backgroundColor = nonSelectedButtonColor;
     }

     [sender setBackgroundColor:[UIColor redColor]];

}
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top