UISlider values - objective-c

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.

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

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.

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.

Related

unable to hook to a ui view inside interface

I am using THEOS, and I know how to hook to methods and override and stuff. But now what I want to do is, I want to access the UI views that are declared within (#interface).
Here is the header file I am referring to:
#class NSString, UILabel, UINavigationBar, UISwitch;
__attribute__((visibility("hidden")))
#interface SettingsViewController : UIViewController <UINavigationBarDelegate, UIBarPositioningDelegate>
{
UINavigationBar *navBar;
UISwitch *alertSwitch;
UISwitch *lightSwitch;
UISwitch *lockSwitch;
UISwitch *infoSwitch;
UILabel *alertLabel;
UILabel *lightLabel;
UILabel *lockLabel;
UILabel *showInfoLabel;
UILabel *infoLabel;
}
#property(retain, nonatomic) UILabel *infoLabel; // #synthesize infoLabel;
#property(retain, nonatomic) UILabel *showInfoLabel; // #synthesize showInfoLabel;
#property(retain, nonatomic) UILabel *lockLabel; // #synthesize lockLabel;
#property(retain, nonatomic) UILabel *lightLabel; // #synthesize lightLabel;
#property(retain, nonatomic) UILabel *alertLabel; // #synthesize alertLabel;
#property(retain, nonatomic) UISwitch *infoSwitch; // #synthesize infoSwitch;
#property(retain, nonatomic) UISwitch *lockSwitch; // #synthesize lockSwitch;
#property(retain, nonatomic) UISwitch *lightSwitch; // #synthesize lightSwitch;
#property(retain, nonatomic) UISwitch *alertSwitch; // #synthesize alertSwitch;
#property(retain, nonatomic) UINavigationBar *navBar; // #synthesize navBar;
- (void).cxx_destruct;
- (void)didReceiveMemoryWarning;
- (void)dismissSettingsViewController;
- (long long)preferredStatusBarStyle;
- (void)showDevInfo;
- (void)infoSwitchSwitched;
- (void)lockSwitchSwitched;
- (void)lightSwitchSwitched;
- (void)alertSwitchSwitched;
- (void)viewDidLoad;
- (long long)positionForBar:(id)arg1;
// Remaining properties
#property(readonly, copy) NSString *debugDescription;
#property(readonly, copy) NSString *description;
#property(readonly) unsigned long long hash;
#property(readonly) Class superclass;
#end
I want to access the UI views under #interface (Ex: UISwitch *alertSwitch;).
How can I access it? Please help I am trying to figure it out since three days :(. Any help or suggestion is very very appreciated.
I hope this help
%hook SettingsViewController
-(void) WhereEverYouWantToRunYourCode {
self.alertSwitch = ....
}
%end

Can not set text for Label

In controller I have label property:
#interface BallsViewController : UIViewController <UIInteraction>
#property (weak, nonatomic) IBOutlet UILabel *ScoreLabel;
-(void)UpdateScore:(int)score;
#end
#interface BallsViewController ()
#end
#implementation BallsViewController
#synthesize ScoreLabel;
- (void)viewDidLoad
{
[super viewDidLoad];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(void)UpdateScore:(int)score
{
NSString *scoreStr =[NSString stringWithFormat:#"%d",score];
[self.ScoreLabel setText:scoreStr];
[self.InfoLabel setText:scoreStr];
}
#end
UpdateScore is protocol method. When I want to set text to ScoreLabel it have value : ScoreLabel UILabel * 0x00000000
It means that it is not initialize?
When I set text, on ui it not change.
Try setting your Label as Strong.
#property (strong, nonatomic) IBOutlet UILabel *ScoreLabel;

Change the green border for an embedded scanner (ZBar)

So I have a ZBarView within a UIViewController (for an embedded scanner) and I was wondering how I could change the Green Border to have it something like this?
#import "ZBarSDK.h"
#interface ScanViewController : UIViewController <ZBarReaderViewDelegate>
#property (nonatomic, strong) IBOutlet ZBarReaderView *readerView;
#property (strong, nonatomic) IBOutlet UILabel *scannedMachineLabel;
#end
- (void)viewDidLoad
{
[super viewDidLoad];
_readerView.readerDelegate = self;
// ensure initial camera orientation is correctly set
UIApplication *app = [UIApplication sharedApplication];
[_readerView willRotateToInterfaceOrientation: app.statusBarOrientation
duration: 0];
}

xCode: Passing a Label text into a custom added subview

I have rewritten this to try and make it more descriptive with more code hopefully:
I have set up a separate UIView class called: pageTitleView code below:
header file:
#import <UIKit/UIKit.h>
#interface pageTitleView : UIView
#property (nonatomic, strong) IBOutlet UILabel *pageTitle;
#property (nonatomic, strong) IBOutlet UILabel *subTitle;
#end
The M File:
#implementation pageTitleView
#synthesize pageTitle;
#synthesize subTitle;
- (id)initWithFrame:(CGRect)frame{
pageTitle = [[UILabel alloc] initWithFrame:CGRectMake(0,labelPosY,300,20)];
pageTitle.textColor = txtColour;
pageTitle.backgroundColor = [UIColor clearColor];
pageTitle.textAlignment = NSTextAlignmentCenter;
pageTitle.font = [UIFont systemFontOfSize:14];
// pageTitle.text to be set by parent view
[self addSubview:pageTitle];
}
With in my parent view controller I have the following:
The Header File:
#import <UIKit/UIKit.h>
#interface UsingThisGuide : UIViewController
#property (strong, nonatomic) UIView *PageTitleBlock;
#property (strong, nonatomic) UIWebView *dispalyPageContent;
#property (strong, nonatomic) UIView *FooterBar;
#end
In the M file i have the following:
#import <QuartzCore/QuartzCore.h>
#import "pageTitleView.h"
#import "MyFirstView.h"
#interface MyFirstView ()
#end
#implementation MyFirstView {
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad {
_PageTitleBlock = [[pageTitleView alloc] initWithFrame:CGRectMake(0, 0, 300, 50)];
_PageTitleBlock.layer.cornerRadius = 5;
_PageTitleBlock.pageTitle.text = #"This should work but not";
[self.view addSubview:_PageTitleBlock];
[super viewDidLoad];
}
#end
No what i want to do is set the pageTitle.text from the pageTitleView class via its parent controller MyFirstView using something like _PageTitleBlock.pageTitle.text= but this is the error i am getting:
Property 'pageTitle' not found on object of type 'UIView *
Yes, you can do that easily, for example, using properties.
In header file of TitleBlock (or pageTitleView) class in #interface section before #end you should define property:
#property (nonatomic, retain) UILabel pageTitle;
or for ARC-projects
#property (nonatomic, strong) UILabel pageTitle;
In parent view controller you should initiate _PageTitleBlock with frame:
_PageTitleBlock = [[pageTitleView alloc] initWithFrame:CGRectMake(10, 10, 200, 200)]; // specify needed frame
// and add it to root view:
[self.view addSubview:_PageTitleBlock];
Now you could access pageTitle property:
_PageTitleBlock.pageTitle.text = #"Text of page title label";
Hope it will help you.
P.S. For class names it is better to use сapitalized names, i.e., PageTitleView rather then pageTitleView.
The problem here, is that you are declaring your property called PageTitleBlock of type UIView in UsingThisGuide, but then call pageTitle which is declared in the class pageTitleView. The compiler doesn't trust this.
Change the type of PageTitleBlock from UIView to pageTitleView and you're good to go!

UITextField: set text from a label

I'm trying to write my first iPad app using Xcode 4: it's based on "Tabbed Application" template with two views. On the first view, user selects a City (label displays selection) and on the second wiew there is a IBOutletCollection(UITextField) NSArray *playersData. I want to set the city selected on first view as a default city on second view. I have checked storyboard connections and they seem to be ok.
I get nothing. Any idea?
First view :
#import
#interface pruebaF1FirstViewController : UIViewController
- (IBAction)selectCity:(UIButton *)sender;
#property (weak, nonatomic) IBOutlet UILabel *citySelected;
#end
First view implementation :
#import "pruebaF1FirstViewController.h"
#import "pruebaF1SecondViewController.h"
#interface pruebaF1FirstViewController ()
#property pruebaF1SecondViewController *secondView;
#end
#implementation pruebaF1FirstViewController
#synthesize citySelected;
#synthesize secondView;
- (void)viewDidLoad
...
- (IBAction)selectCity:(UIButton *)sender {
NSString *currentCity =[sender currentTitle];
citySelected.text=#"";
citySelected.text =[citySelected.text stringByAppendingString:currentCity];
/*writes null*/
secondView.defaultCity.text=[NSString stringWithFormat:#"%#" ,currentCity];
NSLog(#"%#",secondView.defaultCity.text);
}
#end
Second view header
#import <UIKit/UIKit.h>
#interface pruebaF1SecondViewController : UIViewController <UITextFieldDelegate>
#property (strong, nonatomic) IBOutletCollection(UITextField) NSArray *playersData;
#property (retain, nonatomic) IBOutlet UITextField *defaultCity;
- (IBAction)eraseData:(UIButton *)sender;
- (IBAction)savePlayersData:(UIButton *)sender;
- (IBAction)termsPopUp:(UIButton *)sender;
/*- (void)writeDefaultCity:(NSString *)currentCity;*/
#end
Second view implementation
#import "pruebaF1SecondViewController.h"
#import "pruebaF1FirstViewController.h"
#interface pruebaF1SecondViewController ()
#property (nonatomic, strong)pruebaF1FirstViewController *prueba;
#end
#implementation pruebaF1SecondViewController
#synthesize playersData;
#synthesize defaultCity;
#synthesize prueba;
- (void)viewDidLoad
{
[super viewDidLoad];
}
- (void)viewDidUnload
{
[self setPlayersData:nil];
[self setDefaultCity:nil];
[super viewDidUnload];
}
/* Return or Done button dismiss keyboard*/
-(BOOL)textFieldShouldReturn:(UITextField *)boxes
{
for (UITextField *boxes in playersData) {
[boxes resignFirstResponder];
}
return YES;
}
....
/*Trying to get city's name from first view in two ways*/
/*-(void)setDefaultCity
{
NSString *defecto=prueba.citySelected.text;
self.defaultCity.text = defecto;
}*/
- (IBAction)eraseData:(UIButton *)sender {
for (UITextField *boxes in playersData) {
boxes.text = #" ";
}
}
/*
-(void)writeDefaultCity:(NSString *)currentCity
{
defaultCity.text =[NSString stringWithFormat:#"%#" ,currentCity];
NSLog(#"Ciudad elegida: %#",currentCity);
}*/
....
#end
Views are generally not loaded until they are displayed on the device, and may be unloaded at any time when not visible to save memory. This means that when you're setting the 'text' property of the 'defaultCity', that label has not yet been created.
Instead you should add a NSString * property to pruebaF1SecondViewController and set the value of the defaultCity label in viewDidLoad:
In the header:
#property (nonatomic, weak) UILabel *defaultCityLabel;
#property (nonatomic, strong) NSString *defaultCity;
In the implementation
- (void)viewDidLoad
{
[super viewDidLoad];
defaultCityLabel.text = defaultCity;
}
And in the first view controller, assign the string value instead of the label value.