UISlider's value is not shown by UILabel - uilabel

In my app, I have a Settings view, where the user can use UISlider to set value from 50 to 1000. The selected value is then shown in a UILabel.
The problem is that the label does not display at all. The Action and the IBOutlets are connected in IB. The NSLog displays the value correctly on the console.
I can't figure out what is going on....help is needed :)
I have the following:
IBOutlet UILabel *sliderLabel;
IBOutlet UISlider *slider;
...
#property (nonatomic, retain) UILabel *sliderLabel;
#property (nonatomic, retain) UISlider *slider;
-(IBAction) sliderValueChanged:(id) sender;
and
#synthesize sliderlabel, slider;
- (IBAction) sliderValueChanged:(UISlider *) sender {
NSString *sliderValue = [NSString stringWithFormat:#"%d feet",(int)[sender value]];
NSLog(#"%#", sliderValue);
[[self sliderLabel] setText: sliderValue];
}
Thanks, and regards.

Related

Array of UIButtons, can't change button image

I have an array of UIButton objects (I have 15 buttons so I wanted to store them in an array for easy processing). I want to be able to iterate through the array and change the background image/image that the button uses. This is how I currently have it set up:
#property (nonatomic, weak) UIImage *greenLight;
#property (nonatomic, weak) UIImage *yellowLight;
#property (nonatomic, weak) UIImage *redLight;
#property (weak, nonatomic) IBOutlet UIButton *task1Button;
#property (weak, nonatomic) IBOutlet UIButton *task2Button;
#property (weak, nonatomic) IBOutlet UIButton *task3Button;
#property (nonatomic, copy) NSMutableArray *buttonArray;
- (instancetype)init
{
self = [super init];
if(self){
[self.buttonArray addObject:self.task1Button];
[self.buttonArray addObject:self.task2Button];
[self.buttonArray addObject:self.task3Button];
self.greenLight = [UIImage imageNamed:#"greenlight.ICO"];
self.yellowLight = [UIImage imageNamed:#"yellowlight.ICO"];
self.redLight = [UIImage imageNamed:#"redlight.ICO"];
NSLog(#"init complete...");
}
return self;
}
Then I have the following in viewWillAppear
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
[self.task1Button setImage:self.redLight forState:UIControlStateNormal];
[(UIButton *)self.buttonArray[1] setImage:self.greenLight forState:UIControlStateNormal];
}
The second line in viewWillAppear works and changes the button's image. The third line, however, does not. The debugger shows the code reaching that line and not throwing any errors, so I don't know why setting the button image when the button is in an array would not work.
Any ideas?
Try replacing last line in your answer with this :
[(UIButton *)[self.buttonArray objectAtIndex:1] setImage:self.greenLight forState:UIControlStateNormal];
But i'm doing this without compiler so i'm not really sure about where to place the first bracket [
Tell me if it works, i'm not totally sure about it.

UIButton removeFromSuperview

I went through guide from Apple "Your first iOS app"
and now I have a button, which is not declared in ViewController:
#interface HelloWorldViewController : UIViewController <UITextFieldDelegate>
- (IBAction)changeGreeting:(id)sender;
#property (weak, nonatomic) IBOutlet UITextField *textField;
#property (weak, nonatomic) IBOutlet UILabel *label;
#property (copy, nonatomic) NSString *userName;
#end
Now i can remove label (and textField), using [label removeFromSuperview]; but i dont understand how to do it with button. Can someone help?
You should add an IBOutlet to the button as you did for the textfield and the label:
#property (weak, nonatomic) IBOutlet UITextField *textField;
#property (weak, nonatomic) IBOutlet UILabel *label;
#property (weak, nonatomic) IBOutlet UIButton *button; // Don't forget to link to this from Interface Builder
// ...
Then you can remove the button using:
[button removeFromSuperview];
Also note that the tutorial you linked to says:
The sender parameter in the action method refers to the object that is sending the action message (in this tutorial, the sender is the button).
So if you want to remove the button when it is tapped (inside changeGreeting:), then you don't need the IBOutlet because you already have a reference to the button in the sender parameter:
- (IBAction)changeGreeting:(id)sender
{
UIButton *button = (UIButton *)sender;
// ...
[button removeFromSuperview];
// ...
}
You need to declare the button in the controller like you did as an IBAction and this time declare this as an Outlet(IBoutlet).. this way you will get its reference in code..
Alternatively .. you can set a tag to the button in Interface Builder
..
and then retrieve in code using viewWithTag: method

Why I cannot set text property for a UILabel?

I've decleared a UILabel in my view controller
#interface ViewController : UIViewController
{
UILabel *statusText;
}
#property (nonatomic,retain) IBOutlet UILabel *statusText;
-(IBAction)buttonPressed:(id)sender;
#end
Now I want the UILabel's title to change when my button is pressed, but my statusText can not set the properly. for example:
- (IBAction)buttonPressed:(id)sender{
NSString *title = [sender titleForState:UIControlStateNormal];
NSString *newText = [[NSString alloc]initWithFormat:#"%#button pressed",title];
statusText.text = #"helloworld";
NSLog(#"%#",statusText.text);
[newText release];
}
the log returns
2012-01-25 00:46:27.663 buttonfun[34247:f803] (null)
It means that the text property "helloworld" has not been set. Why?
Try logging this instead:
NSLog(#"%#",statusText);
If it's also null, it means you haven't hooked up your statusText IBOutlet to your UILabel in Interface Builder (you need to ctrl-drag from your File's Owner to the label and then select statusText).

Simple slider app, quick question

Everything looks right, and yet something is missing. Could you please take a look?
.m
#synthesize textLabel;
#synthesize slider;
- (IBAction)setLabel:(id)sender {
int res = [slider value];
NSLog(#"something changed %d", res);
[textLabel setText:[NSString stringWithFormat:#"%d",[slider value]]];
}
.h
#interface SlidaAppDelegate : NSObject {
UILabel *textLabel;
UISlider *slider;
NSString *text;
}
- (IBAction)setLabel:(id)sender;
#property (nonatomic, retain) IBOutlet UIWindow *window;
#property (nonatomic, retain) IBOutlet UILabel *textLabel;
#property (nonatomic, retain) IBOutlet UISlider *slider;
Whenever slider moves, i see value update correctly, however simulator shows strange numbers
Simulator shows
Xib setup is as follows:
Please advise
*UPDATE <---------------------------------------- *
Problem went away when i changed
[textLabel setText:[NSString stringWithFormat:#"%d",[slider value]]];
to
[textLabel setText:[NSString stringWithFormat:#"%d",res]];
I don't fully understand why that it though, considering that
int res = [slider value];
If possible, please explain
My bet is that value returns a float which is in between 0 and 1. So when you assign it to an int, it gets rounded down to 0.
Make res a float.
EDIT
%d wants an int, you were giving it a float with [slider value]. So it must have interpreted that value in a strange way. See DOC

specifying textField using UITextField delegate

Hi i'm new to software for iphone/ipad apps.
I am using the textField delegate method:
"(BOOL)textFieldShouldReturn:(UITextField *)textField".
I have gotten to the point where when the user enter a name and presses 'enter' and a label displays what the user just wrote. This is just two lines of code like so:
labelChange.text = textField.text; //labelChange is my label in IB
return YES;
Since i have several textfields in IB, this code works for all of them. I'm not sure how to be specific with the code and have this label change for only ONE of my textFields.
My .h files looks like this.
#interface FirstViewController : UIViewController <UITextFieldDelegate> {
IBOutlet UILabel *labelChange;
IBOutlet UITextField *userName;
IBOutlet UITextField *homeValue;
IBOutlet UITextField *downPayment;
IBOutlet UITextField *textField;
}
#property (nonatomic,retain) UILabel *labelChange;
#property (nonatomic,retain) UITextField *userName;
#property (nonatomic,retain) UITextField *homeValue;
#property (nonatomic,retain) UITextField *downPayment;
#property (nonatomic,retain) UITextField *textField;
#end
I want my label to change only when user types in the text field labeled "userName".I'm not sure how to do this, am I missing something?In IB, I connected all my textfield delegates to 'Files Owner'. Any advice would be really helpful. thanks!
Assign each of your text fields a tag number i.e.
textField1.tag = 1
textField2.tag = 2
etc...
Then in your -(BOOL)textFieldShouldReturn:(UITextField *)textField you can do a:
switch (textField.tag) {
case 1:
labelChange1.text = textField.text;
break;
case 2:
labelChange2.text = textField.text;
break;
etc... etc...
}