It was a very strange process.
I have an IBOutletCollection UIButtons. I scroll through the collection and create it like this ( displayHourButtons is called from viewWillAppear ):
- (void)displayHourButtons { // Counter NSUInteger b = 0; // Set attributes UIFont *btnFont = [UIFont fontWithName:@"Metric-Semibold" size:13.0]; UIColor *btnTextColor = [UIColor colorWithRed:(147/255.0f) green:(147/255.0f) blue:(147/255.0f) alpha:1.0]; NSNumber *btnTracking = [NSNumber numberWithFloat:0.25]; NSMutableParagraphStyle *btnStyle = [[NSMutableParagraphStyle alloc] init]; [btnStyle setLineSpacing:2.0]; NSDictionary *btnAttrs = [NSDictionary dictionaryWithObjectsAndKeys: btnFont, NSFontAttributeName, btnTextColor, NSForegroundColorAttributeName, btnTracking, NSKernAttributeName, nil]; // CREATE THE BUTTONS for (UIButton *hourButton in hourButtons) { // I'm using the attributed string for something else // later in development that I haven't got to yet. // I simplified the string for this example sake. NSString *btnTitleText = [NSString stringWithFormat:@"Button %lu", (unsigned long)b]; NSMutableAttributedString *attributedText = [[NSMutableAttributedString alloc] initWithString:btnTitleText attributes:btnAttrs]; [attributedText addAttribute:NSParagraphStyleAttributeName value:btnStyle range:NSMakeRange(0, btnTitleText.length)]; CALayer *btnLayer = [hourButton layer]; [btnLayer setMasksToBounds:YES]; [btnLayer setCornerRadius:19.0f]; [hourButton setTag:b]; [hourButton setContentEdgeInsets:UIEdgeInsetsMake(5.0, 1.0, 0.0, 0.0)]; [hourButton setAttributedTitle:attributedText forState:UIControlStateNormal]; [hourButton setContentHorizontalAlignment:UIControlContentHorizontalAlignmentCenter]; [hourButton setContentVerticalAlignment:UIControlContentVerticalAlignmentCenter]; hourButton.titleLabel.lineBreakMode = NSLineBreakByWordWrapping; [hourButton addTarget:self action:@selector(showHour:) forControlEvents:UIControlEventTouchUpInside]; b++; } }
When one of the buttons is pressed, the showHour: action is called:
- (IBAction)showHour:(id)sender { [self.hourButtons enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) { UIButton *button = (UIButton *)obj; if (button != sender && button.enabled) {
I tried all kinds of crazy things to make UIImage be in the selected state, but nothing worked. This transaction enumerateObjects is the only thing that worked. That is why I say that it was a strange process. I think the buttons do not remain active indefinitely?
In any case, MY QUESTION : Is there a definite reason why the color of the title does not change? Just the background? I suspect that this has something to do with the fact that the background was not set initially, but I could not explain why.
Thanks!
UPDATED
In response to @Timothy Moose, below is the updated code.
- (IBAction)showHour:(id)sender { [self.hourButtons enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) { UIButton *button = (UIButton *)obj;