Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialerickwatson
1,783 PointsSame Button Text Color problem...
I'm having same issue with button text color refusing to change last the first poster for this video. My code is exactly the same as the video AND exactly the same as the code cut in at the first question.
One of the answers given, re-drag the button to make the UIOutlet over again was the first thing I tried. I have done it twice to no avail. There was a suggestion to 'change to default'. Not sure what that is, but if it is to make sure the button's color is set to 'default' in the Attributes Inspector, I've done that as well.
The code to change the background color is the same as that used to change the button color (apart from the specific object referenced obviously) and the background IS working correctly. So I am completely at a loss. This looks like a perfect thing for the following debugging videos to help me with, but this problem is not causing the app to crash. It is simply a small aesthetic failure, nonetheless driving me nuts!
5 Answers
Chris Reilly
12,086 PointsHi Erick,
Not sure if you're still experiencing this problem since you last posted here 11 months ago, but just in case you or anyone else is experiencing this problem (as I was just now), what I had to do to fix it was:
- Go to Main.storyboard
- Select the button
- In the Attributes inspector, under the View section, set the Tint to "Default." Mine was set to the same color as the starting background color.
Anyway, not sure if anyone else has the issue, but this is what turned out to solve the issue for me!
Chris
Damien Watson
27,419 PointsHey Erick, Assuming that the button is correctly attached to the ViewController, it may be the way you are setting the color. Can you show us that code?
You have to use the 'setTitleColor:forState:' method, this also gives you better control over each state.
// Won't work
myButton.titleLabel.textColor = [UIColor redColor];
// Will colorize
[myButton setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
erickwatson
1,783 PointsViewController.h
import <UIKit/UIKit.h>
@class FactBook; @class ColorWheel;
@interface ViewController : UIViewController
@property (weak, nonatomic) IBOutlet UILabel *funFactLabel; @property (strong, nonatomic) FactBook *factBook; @property (strong, nonatomic) ColorWheel *colorWheel; @property (weak, nonatomic) IBOutlet UIButton *FunFactButton;
@end
ViewController.m#import "ViewController.h"
import "FactBook.h"
import "ColorWheel.h"
@interface ViewController ()
@end
@implementation ViewController
-
(void)viewDidLoad { [super viewDidLoad];
self.factBook =[[FactBook alloc] init]; self.colorWheel =[[ColorWheel alloc] init];
UIColor *randomColor = [self.colorWheel randomColor]; self.view.backgroundColor = randomColor; self.FunFactButton.tintColor = randomColor; self.funFactLabel.text = [self.factBook randomFact]; }
(void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. }
(IBAction)showFunFact { UIColor *randomColor = [self.colorWheel randomColor]; self.view.backgroundColor = randomColor; self.FunFactButton.tintColor = randomColor; self.funFactLabel.text = [self.factBook randomFact];
}
@end
erickwatson
1,783 PointsSorry. Here.
ViewController.h
#import <UIKit/UIKit.h>
@class FactBook;
@class ColorWheel;
@interface ViewController : UIViewController
@property (weak, nonatomic) IBOutlet UILabel *funFactLabel;
@property (strong, nonatomic) FactBook *factBook;
@property (strong, nonatomic) ColorWheel *colorWheel;
@property (weak, nonatomic) IBOutlet UIButton *FunFactButton;
@end
ViewController.m
#import "ViewController.h"
#import "FactBook.h"
#import "ColorWheel.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.factBook =[[FactBook alloc] init];
self.colorWheel =[[ColorWheel alloc] init];
UIColor *randomColor = [self.colorWheel randomColor];
self.view.backgroundColor = randomColor;
self.FunFactButton.tintColor = randomColor;
self.funFactLabel.text = [self.factBook randomFact];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)showFunFact {
UIColor *randomColor = [self.colorWheel randomColor];
self.view.backgroundColor = randomColor;
self.FunFactButton.tintColor = randomColor;
self.funFactLabel.text = [self.factBook randomFact];
}
@end
erickwatson
1,783 PointsI'm kinda hoping the answer can be 'here's where you went wrong in your code', or 'new version of Xcode means this has to be changed'. If there are chunks of code that literally have to be added to this to make it work then the lesson is wrong. Seems to me the lesson must work as written and I am missing something otherwise every person taking this would have it fail. And the instructor's example does work as he runs it in the video.
Damien Watson
27,419 PointsHaha, I wish it was something like that as well :)
Try my suggestion below, your code works fine, so it has to be the connection somehow.
Damien Watson
27,419 PointsI have pasted your code in and it works fine. If you paste the below line in after all the others in 'viewDidLoad' it will definitely show whether or not the button is connected:
self.FunFactButton.backgroundColor = [UIColor redColor];
The only other thing I can suggest (and I know you've redone these connections) is go to the storyboard and right click on the button. The button should have:
- 'Touch Up Inside' linked to View Controller - showFunFact
- Referencing Outlets 'FunFactButton' - View Controller
Christian A. Castro
30,501 PointsChristian A. Castro
30,501 PointsChris Reilly thank you!! I was experiencing the same problem even knowing everything was okay. It was just to set "Tint" to Default. Thank you so much.
Eduard Edwin
14,569 PointsEduard Edwin
14,569 PointsThanks for the answer. That's a great life saver! I had the same problem with mine as well. Changing the tint property to Default really solved the problem.