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

UIButton tintColor

Hi I am trying to set the colour of my Round rect buttons in my code but it doesn't seem to be working, the buttons only change to the desired (default) colour when they are pressed. Anyone got any ideas? This is an example line:

self.startButton.tintColor= GREEN_COLOUR;

Thanks Larry

9 Answers

Amit Bijlani
STAFF
Amit Bijlani
Treehouse Guest Teacher

Tint color is a property of class UIColor so you need to provide it with a UIColor object. Not sure where you got GREEN_COLOUR from.

self.startButton.tintColor= [UIColor greenColor];

Hi Amit, Nice to have some comm's with you after watching your many tutorials.

I made a constants file as per your tutorial on iOS Foundations/Appearance :) . I tried that code that you sent also and it had the same result :( , I take it I have done something else wrong?

Also is there a way to pause an NSTimer? I am trying to create a stopwatch app to gain a better understanding & I have a Start, Stop & Reset button. Start creates & fires the timer ok Reset invalidates & nils the timer ok but if I use the stop just to invalidate the running timer a restart crashes the app.

Thanks for your help.

Larry

Amit Bijlani
STAFF
Amit Bijlani
Treehouse Guest Teacher

A constants file makes sense! Usually people just copy / paste code from they find on the web.

Here's the thing with UIButton, if it is declared of the type UIButtonTypeRoundedRect then you can't change the tint color. To change the color you need define it as UIButtonTypeCustom. If you have the button on the storyboard then simply change it using the Utility Area > Attributes inspector.

Regarding the NSTimer issue can you paste your code? Or better yet create another discussion thread with that issue so we don't confuse the two discussions.

PS. Nice talking to you too :)

Amit, Re:Button Colour Just to make it clear to me, if I change the button type in the storyboard from Rounded Rect button to Custom & keep the code: self.startButton.tintColor= [UIColor greenColor];

or as I had it ref my constant file, then it should work? Or do I have to completely define the button?

Thanks Larry

Amit Bijlani
STAFF
Amit Bijlani
Treehouse Guest Teacher

Changing the button type in the storyboard and keeping your code should work. You can reference your constants file too because the problem is with the button type and not your code.

Changing the type didn't work :( , can you take a quick look at the code?

#import "FunctionalViewController.h"

@interface FunctionalViewController ()
@end

@implementation FunctionalViewController

@synthesize counter;
@synthesize seconds;
@synthesize mSeconds;
@synthesize myTimer;
@synthesize startButton;
@synthesize stopButton;
@synthesize resetButton;






- (void) count{
    float sec;
int val1, val2;
counter++;

    val1 = counter /10;
    val2 = fmod (counter,val1);
    NSLog(@"VAl2 =%i",val2);
    sec = val1 + (val2 /10);


    if (sec <= 0.0) {
        seconds.text = [NSString stringWithFormat:@"00."];
            mSeconds.text = [NSString stringWithFormat:@"00 Sec"];
}else{
    seconds.text = [NSString stringWithFormat:@"%02i.", val1];
    mSeconds.text = [NSString stringWithFormat:@"%02i Sec",val2];

}
NSLog(@"Counter = %i", counter);
}


- (IBAction)start:(id)sender {
    counter = 0;

    if (!myTimer){
myTimer = [NSTimer   scheduledTimerWithTimeInterval:0.10
                                               target:self
                                             selector:@selector (count)
                                             userInfo:nil
                                              repeats:YES];
    }

    else{
    [myTimer fire];
    }
    seconds.text = [NSString stringWithFormat:@"00."];
    mSeconds.text = [NSString stringWithFormat:@"00 Sec"];
    self.seconds.textColor = GREEN_COLOUR;
    self.mSeconds.textColor = GREEN_COLOUR;
    NSLog(@"Counter %i", counter);
    NSLog(@"MyTimer %@",myTimer);

}

- (IBAction)stop:(id)sender {

    [myTimer invalidate];
    myTimer = nil;    
    //    NSLog(@"Counter %i", counter);
    self.seconds.textColor = [UIColor blackColor];
    self.mSeconds.textColor = [UIColor blackColor];

}

- (IBAction)Reset:(id)sender {
    if (counter > 0) {
        [myTimer invalidate];
        myTimer = nil;
    }
}



- (void)viewDidLoad
{
   [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    //Background Images:
   [self.view setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"steel-mesh.png"]]];

    //Button Colours:
    self.startButton.tintColor= GREEN_COLOUR;
    self.stopButton.tintColor= RED_COLOUR;
    self.resetButton.tintColor= BLUE_COLOUR;

    //Text attributes
        seconds.text = [NSString stringWithFormat:@"00."];
    mSeconds.text = [NSString stringWithFormat:@"00 Sec"];
    seconds.backgroundColor = [UIColor grayColor];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
Amit Bijlani
STAFF
Amit Bijlani
Treehouse Guest Teacher

Sorry it's backgroundColor not tintColor

self.startButton.backgroundColor= GREEN_COLOUR;
Amit Bijlani
STAFF
Amit Bijlani
Treehouse Guest Teacher

Few things about the code.

  1. Properties must be referred to as self.property.
  2. You no longer need to specify @synthesize
- (IBAction)start:(id)sender {
    counter = 0;

   // if there is no timer object then create it
    if (!self.myTimer){
self.myTimer = [NSTimer   scheduledTimerWithTimeInterval:0.10
                                               target:self
                                             selector:@selector (count)
                                             userInfo:nil
                                              repeats:YES];
    }

   // You don't need an else because you want to fire it regardless
    [self.myTimer fire];
    self.seconds.text = [NSString stringWithFormat:@"00."];
    self.mSeconds.text = [NSString stringWithFormat:@"00 Sec"];
    self.seconds.textColor = GREEN_COLOUR;
    self.mSeconds.textColor = GREEN_COLOUR;
    NSLog(@"Counter %i", counter);
    NSLog(@"MyTimer %@",myTimer);

}

- (IBAction)stop:(id)sender {

  // You need to test if the timer is still valid before invalidating it
   if ( [self.myTimer isValid] {
        [self.myTimer invalidate];
       self.myTimer = nil;
   }

    //    NSLog(@"Counter %i", counter);
    self.seconds.textColor = [UIColor blackColor];
    self.mSeconds.textColor = [UIColor blackColor];

}

Amit, Thanks for the tips, I will try and see if I can work out this timer pause feature, I think I have a solution. I am sure I will be in contact with more silly mistakes in the future ;) , in the meantime if your ever in Ireland drop me a line. Best regards, Larry