如何在点击动态UIButton时改变标题颜色和背景颜色,第一个按钮应该被默认选中



我有一个视图,我以编程方式创建了一些8个动态按钮。按钮的标题颜色为白色。我想在点击按钮时将按钮标题的颜色更改为绿色。如果我点击任何其他按钮,之前点击的按钮标题颜色应该变成白色,当前按钮标题颜色应该变成绿色。

我已经使用了下面的代码和它的工作正常更改前一个按钮的颜色,现在我想要的第一个按钮是在选择模式作为默认值,第一个按钮应该改变时,另一个按钮被点击。

- (void)viewDidLoad
{
_examsListDict1 = [[NSMutableDictionary alloc]init];
_examsListDict1[@"Exam Name"] = @"JEE Mains";
.........so on another 4 dictionaries
_examsListArray = [[NSMutableArray   
alloc]initWithObjects:_examsListDict1,_examsListDict2,_examsListDict3,_examsListDict4,_examsListD
ict5, nil];
NSUInteger i;
int xCoord=0;
int yCoord=0;
int buttonWidth=120;
int buttonHeight=50;
int buffer = 1;
for (i = 0; i <[_examsListArray count]; i++)
{
    aButton = [UIButton buttonWithType:UIButtonTypeCustom];
    aButton.frame     = CGRectMake(xCoord, yCoord,buttonWidth,buttonHeight );
    [aButton setTitle:[[_examsListArray objectAtIndex:i] objectForKey:@"Exam Name"]       
    forState:UIControlStateNormal];
    [aButton setTag:i];
    [aButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [aButton setBackgroundColor:[UIColor colorWithRed:250.0f/255.0f green:255.0f/255.0f 
    blue:221.0f/255.0f alpha:1]];
    [aButton addTarget:self action:@selector(whatever:) 
    forControlEvents:UIControlEventTouchUpInside];
    [_buttonsHorizontalScrollView addSubview:aButton];
     xCoord += buttonWidth + buffer;
    }
    [_buttonsHorizontalScrollView   
    setContentSize:CGSizeMake(aButton.frame.size.width*_examsListArray.count+203, yCoord)];
    }
    - (void)whatever:(id)sender
   {
   UIButton *tempBtn = (UIButton *)sender;
   [aButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
   [aButton setBackgroundColor:[UIColor colorWithRed:250.0f/255.0f green:255.0f/255.0f 
   blue:221.0f/255.0f alpha:1]];
   aButton = tempBtn;
   [aButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
   [aButton setBackgroundColor:[UIColor colorWithRed:133.0f/255.0f green:169.0f/255.0f 
   blue:83.0f/255.0f alpha:1]];
   }

不,我只想让第一个按钮在加载视图时作为默认选择,每当我选择另一个按钮时,它应该改变为取消选择的颜色。

   if ([aButton tag] == 0)
 {
  [aButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal]; 
  [aButton setBackgroundColor:[UIColor colorWithRed:133.0f/255.0f green:169.0f/255.0f 
  blue:83.0f/255.0f alpha:1]];
  }

我已经使用上面的代码来选择作为默认的第一个按钮,但是当单击其他按钮时,第一个按钮不会改变,只有当第一个按钮再次单击时,它才会改变,因为这个aButton = tempBtn。怎么做呢?

我建议您设置所有按钮的选定状态标题颜色,然后将它们添加到数组中。当你按下其中一个按钮时,只需改变它们的状态,无需处理颜色或跟踪当前选择的按钮。

@property (strong, nonatomic) NSMutableArray *yourButtons;

- (void)viewDidLoad {
    [super viewDidLoad];
    _examsListDict1 = [[NSMutableDictionary alloc]init];
    _examsListDict1[@"Exam Name"] = @"JEE Mains";
    .........so on another 4 dictionaries
    _examsListArray = [[NSMutableArray alloc] initWithObjects:_examsListDict1,_examsListDict2,_examsListDict3,_examsListDict4,_examsListDict5, nil];
    NSUInteger i;
    int xCoord=0;
    int yCoord=0;
    int buttonWidth=120;
    int buttonHeight=50;
    int buffer = 1;

    // Generate background images
    //
    UIImage *backgroundImage = [self imageWithColor:[UIColor colorWithRed:250.0f/255.0f green:255.0f/255.0f blue:221.0f/255.0f alpha:1]];
    UIImage *selectedBackgroundImage = [self imageWithColor:[UIColor colorWithRed:133.0f/255.0f green:169.0f/255.0f blue:83.0f/255.0f alpha:1]];

    // Initialize your array
    //
    self.yourButtons = [[NSMutableArray alloc] init];
    for (i = 0; i < [_examsListArray count]; i++) {
        UIButton *aButton = [UIButton buttonWithType:UIButtonTypeCustom];
        aButton.frame = CGRectMake(xCoord, yCoord,buttonWidth,buttonHeight );
        [aButton setTitle:[[_examsListArray objectAtIndex:i] objectForKey:@"Exam Name"] forState:UIControlStateNormal];
        [aButton setTag:i];
        [aButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [aButton setTitleColor:[UIColor whiteColor] forState:UIControlStateSelected];
        [aButton setBackgroundImage:backgroundImage forState:UIControlStateNormal]
        [aButton setBackgroundImage:selectedColorImage forState:UIControlStateSelected]
        [aButton addTarget:self action:@selector(whatever:) forControlEvents:UIControlEventTouchUpInside];
        [_buttonsHorizontalScrollView addSubview:aButton];
        xCoord += buttonWidth + buffer;
        // Select the first one
        //
        if (i == 0)
            aButton.selected = YES;

        // Add your newly created button to the array
        //
        [self.yourButtons addObject:aButton];
    }
    [_buttonsHorizontalScrollView setContentSize:CGSizeMake(aButton.frame.size.width*_examsListArray.count+203, yCoord)];
}
- (void)whatever:(id)sender {
   for (UIButton *button in self.yourButtons) {
       // Toggle selected flag
       //
       button.selected = button == sender;
   }
}

+ (UIImage *)imageWithColor:(UIColor *)color {
    // http://stackoverflow.com/a/24665476/1835155
    //
    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;
}

true未按下的按钮是uicontrolstatnormal而按下的按钮是UIControlStateSelected所以根据状态设置你想要的颜色。你通常在任何人有机会使用它之前在ViewDidload中这样做-下面是一个示例设置:

 button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button setSelected:YES];
    button.frame = CGRectMake(x, y, width, height);
    [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [button setTitleColor:[UIColor blueColor] forState:UIControlStateSelected];
    [button setTitle:@"Button Title" forState:UIControlStateNormal];
    [button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    [button setBackgroundImage:[UIImage imageNamed:@"button.png"] forState:UIControlStateNormal];
    [button setBackgroundImage:[UIImage imageNamed:@"buttonActive.png"] forState:UIControlStateSelected];
    [button setBackgroundImage:[UIImage imageNamed:@"buttonActive.png"] forState:UIControlStateHighlighted];
    [button setBackgroundImage:[UIImage imageNamed:@"buttonActive.png"] forState:UIControlStateDisabled];

我认为当触发按钮被点击时你应该做的是将状态设置为selected

- (IBAction)touchDown:(id)sender {
  if (sender == trigger_button) {
   [button setHighlighted:TRUE];
   [button setSelected:TRUE];
}

为了在点击另一个按钮时改变第一个按钮的状态,你必须"伪造"一个触摸事件。在另一个按钮的选择器方法中这样做:

例如:

-(void)thirdButtonClicked:(id) sender
{
    [firstButton sendActionsForControlEvents: UIControlEventTouchUpInside];
}

最新更新