Reputation: 11
I'm trying to create an app that has three UISlider used for selecting R, G and B values. The slider should change the colour of a label according to the sliders values.
ViewController.h
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController
{
NSInteger color;
IBOutlet UISlider *redSlider;
IBOutlet UISlider *greenSlider;
IBOutlet UISlider *blueSlider;
IBOutlet UILabel *colorLabel;
}
@property (nonatomic, retain) UISlider *redSlider;
@property (nonatomic, retain) UISlider *greenSlider;
@property (nonatomic, retain) UISlider *blueSlider;
colorLabel.textColor = [UIColor colorWithRed: redSlider.value green:greenSlider.value
blue:blueSlider.value alpha:1];
@end
ViewController.m
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@synthesize redSlider, greenSlider, blueSlider;
@end
This is what I have so far and I'm totally stuck.
Upvotes: 0
Views: 700
Reputation: 3082
Your ViewController.h :-
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController
{
IBOutlet UISlider *redSlider;
IBOutlet UISlider *greenSlider;
IBOutlet UISlider *blueSlider;
IBOutlet UILabel *colorLabel;
}
@property (nonatomic, retain) UISlider *redSlider;
@property (nonatomic, retain) UISlider *greenSlider;
@property (nonatomic, retain) UISlider *blueSlider;
@end
and your ViewController.m will be like this:-
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
@synthesize redSlider, greenSlider, blueSlider;
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[self changeLabelColor];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(Void) changeLabelColor
{
colorLabel.backgroundcolor = [UIColor colorWithRed:redSlider.value green:redSlider.value blue:redSlider.value alpha:1.0];
}
@end
As i didn’t copied paste and i have written it, there might be some typo.. textcolor wouldn’t change label color so i changed it to backgroundcolor. you can also add slider method with IBAction.
Upvotes: 0
Reputation: 77631
You can do this like...
ViewController.h
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController
@end
ViewController.m
#import "ViewController.h"
@interface ViewController ()
// you need to make sure these are actually created
// and give them all a target of self and action sliderValueChanged
@property (nonatomic, retain) UISlider *redSlider;
@property (nonatomic, retain) UISlider *greenSlider;
@property (nonatomic, retain) UISlider *blueSlider;
// not sure where this is set up
@property (nonatomic, strong) UILabel *colorLabel;
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
// You may have to create your labels and sliders in here...
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)sliderValueChanged
{
// the sliders have changed so set the colour of the label
// based on the new values of the sliders.
self.colorLabel.textColor = [UIColor colorWithRed:self.redSlider.value
green:self.greenSlider.value
blue:self.blueSlider.value
alpha:1.0];
}
@end
Upvotes: 3
Reputation: 12093
Because you don't actually have a question I'm going to take a guess that your code doesn't compile, because it wouldn't.
colorLabel.textColor = [UIColor colorWithRed: redSlider.value green:greenSlider.value blue:blueSlider.value alpha:1];
The above line doesn't belong in you interface. I would recommend you change the code you currently have to something like.
ViewController.h
@interface ViewController : UIViewController
// You don't need the ivars anymore these get generated with the properties below
@property (nonatomic, retain) IBOutlet UISlider *redSlider;
@property (nonatomic, retain) IBOutlet UISlider *greenSlider;
@property (nonatomic, retain) IBOutlet UISlider *blueSlider;
@property (nonatomic, retain) IBOutlet UILabel *colorLabel
@property (assign) NSInteger color;
@end
ViewController.m
@implementation ViewController
// Also no need for the synthesize as these also get generated automatically
- (void)viewDidLoad
{
[super viewDidLoad];
// To change the actual color when changing the value of a slider move this to a method that gets called on slider changed.
colorLabel.textColor = [UIColor colorWithRed:self.redSlider.value green:self.greenSlider.value self.blue:blueSlider.value alpha:1];
}
@end
And once you actually ask a question I might also be able to help you with that.
Upvotes: 1