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 trial

iOS Build a Simple iPhone App with Objective-C Improving Our User Interface Using the ColorWheel Class

Button Text Color is not changing

Hi, for some reason my button text isn't randomly changing as it should with my background color text. The app runs properly with no bugs, but the button text does not change as it should in uniform with the background color randomly. Is there anywhere I can look in my code that could possibly fix this? I haven't been able to find any difference between my code and the code within the tutorial. Any help would be greatly appreciated. Thanks!

7 Answers

Hiya,

There's four lines of code here that occur in two places so they're ripe for refactoring into one method, I think.

    UIColor *randomColor = [self.colorWheel randomColor];
    self.view.backgroundColor = randomColor;
    self.funFactButton.tintColor = randomColor;
    self.funFactLabel.text = [self.factBook randomFact];

appears in both viewDidLoad as well as showFunFact

Does re-initialising UIColor randomColor in both places cause the "random" bit to be out of sync?

Not sure - I t might be worth trying to use randomColor without starting from scratch in showFunFact(), perhaps?

Steve.

Dave Ko
Dave Ko
11,915 Points

Figured this one out....For me it had to do with the text label color being selected (that green color) and not being set to default. Change it back to "default". Your code will work then. Hope this helps.

I'm having the same issue and I HAVE re-dragged the button. It sounds like the change back to default is probably my issue here, but I don't know what that means. Change 'it' back to default where?

Thanks.

Dave Ko
Dave Ko
11,915 Points

Did you get it figured out?

Check out this short video cast of the issue: Hope this helps. http://screencast.com/t/zAo81xLH

Didn't help. Thanks for your suggestion though. I DID think that was what was meant with 'default' but no wrangling with that stuff in the Attributes Inspector has made any difference. The button in my file was set to default, but I did try 'un-setting' it and re-setting it anyway. I've re-posted this question on the main question page of the lesson so this doesn't get buried in this thread. Hopefully someone will have an answer. I've moved on to the next videos, but this is really bothering me and getting hung up on something affects my morale and motivation for future classes. Ugh.

Didn't help. Thanks for your suggestion though. I DID think that was what was meant with 'default' but no wrangling with that stuff in the Attributes Inspector has made any difference. The button in my file was set to default, but I did try 'un-setting' it and re-setting it anyway. I've re-posted this question on the main question page of the lesson so this doesn't get buried in this thread. Hopefully someone will have an answer. I've moved on to the next videos, but this is really bothering me and getting hung up on something affects my morale and motivation for future classes. Ugh.

Dave Ko
Dave Ko
11,915 Points

Now you got me interested, share a link with your source code so I can investigate for myself.

Hi Eric,

Can you show the code where the colour is changed as well as the code where the background colour is changed?

I seem to remember having the same problem so will look through my app too.

Thanks,

Steve.

Hi Steve, Thanks for taking a look. Here are both my viewcontroller.h and .m files.

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

@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

Hi Steve, Thanks for the advice. Having the code in both places wasn't the issue, but after trying your method, it made think that it was something I did in prior steps. Turns out; oddly enough, that when I was linking the button to a property in the main.storyboard view the first time, the pop-up window didn't originally recognize it as a button (or UIBotton). Instead of deleting that code and performing the ctrl - drag again, I merely adjusted the code within the property by typing it out. My build didn't appear to have any bugs, but it was almost like that property didn't exist in my viewcontroller.h file. Kind of weird. I am still a noob to xCode, so I guess that is one of those things that the compiler is picky about. Nonetheless, thank you for taking some time to help me out and answer my question, it is very much appreciated. All the best!

Patrick Segarel
Patrick Segarel
2,233 Points

Had a similar issue with the button not changing colour when the app first loaded. After the first click , everything was fine.

Solved the problem by refactoring. It is true that we had the same exact code in two places, so I declared "showFunFact" in ViewController.h

//  ViewController.h
- (IBAction)showFunFact;

then in ViewController.m

//  ViewController.m
- (void)viewDidLoad {
    [super viewDidLoad];

    self.colorWheel = [[ColorWheel alloc] init];
    self.factbook = [[FactBook alloc] init];

    [self showFunFact];
}