Trying to remove decimal from UISlider and text label - ios

I have a uislider connected to a label, when the slider moves, the label adjusts its value. I am able to set the increments and the max and min, but i can't figure out how to remove the decimal places from the value..i simply want whole steps. suggestions please
- (IBAction)sliderValueChanged:(id)sender {
self.myLabel.text = [NSString stringWithFormat:#"%f", (roundf(self.mySlider.value)];
}

Display the result as an int instead of a float.
self.myLabel.text = [NSString stringWithFormat:#"%d", (int)roundf(self.mySlider.value)];

Related

How can I decrease and increase a text value on button-click by multiplying two numeric values?

I have two values on different variables and want to add the multiplication of these values on uitextfield.
For example: the first value is 10 (the default value), and the second value is 30; the second value can be changed because it will always come from API. Now first I add the multiplication of these values on uitextfield.
If I click on the increase button, then the current value of text-field should be added to the second value (30) and be displayed, and so on.
The second thing is that when I click on the decrease button, it should be decreased by the second value (30), and so on.
It's a concept like a shopping cart.
Here is my code, which is not working properly:
- (IBAction)IncreasePack:(id)sender {
[self.TotalPrice setText:[NSString stringWithFormat:#"%d", number]];
NSLog(#"number %d", number);
number = number+firstincrease;
NSLog(#"number %d", number);
packnumber++;
_Packs.text = [NSString stringWithFormat:#"%d",packnumber ];
NSLog(#"packnumber %d", packnumber);
}
- (IBAction)DecreasePack:(id)sender {
packnumber--;
_Packs.text = [NSString stringWithFormat:#"%d", packnumber];
number = number-firstdecrease;
NSLog(#"number %d", number);
[self.TotalPrice setText:[NSString stringWithFormat:#"%d", number]];
}

Changing the text of a label on button press with dynamic integer

I'm following a tutorial to create a simple game where you tap a button and the the game counts how many times you have pressed the button as you go along. The score is displayed on screen in the form of a label.
I can make it so that when you press the button the label text changes to say 'Pressed!'. But then i can not get the label to change when i try to add the changing score with a format specifier.
-(IBAction)buttonPressed{
count ++;
//scoreLabel.text =#"pressed";
scoreLable.text = [NSString stringWithFormat:#"Score\n%i", count];
}
Any help would be greatly appreciated. Thanks!
You may not have your label set up for multiple lines of text. Try getting rid of the "\n" in your format string. Other than that, you need to tell us what happens. Have you put a breakpoint to make sure your button IBAction is being called? Have you checked to make sure "scoreLable" (sic) is not nil? The devil is in the details.
-(IBAction)buttonPressed : (id) sender{
UIButton * btnPressed = (UIButton *)sender;
int buttonValue = [[btnPressed titleForState:UIControlStateNormal] intValue];
NSLog(#"Button value = %d", buttonValue);
//scoreLabel.text =#"pressed";
scoreLable.text = [NSString stringWithFormat:#"Score\n%i", count];
}
First You Can Set static int count = 0;

Adding textfield input to label

I feel like this is a rookie question but I'm stumped. The app will become much more complex than this (subtraction, division, multiplication, math) but in order to work from a foundation... Well, I don't even have a foundation.
-(IBAction)change:(id)sender {
NSString *xText = x.text;
int xValue = [xText intValue];
NSString *yText = y.text;
int yValue = [yText intValue];
int zValue = xValue + yValue;
NSString *zText = [NSString stringWithFormat:#"%d", zValue];
zText = z.text;
Basically what I'm trying to do right now is have two separate text fields collect user-typed numbers. When the "Calculate" button is pressed, the label will change to show the answer. (i.e. x = 4, y = 3, press calculate, z label changes to 7)
My guess is you would like to add the zText to a label?
Simply go:
[label setText:zText];
Where label is the IBOutlet to your label

How to get UISlider to step in decimals

I have a UISlider with a min - 0.1 & max - 1. I want the label displaying the value to display the decimals and not just 0 and 1.
Here is the current code:
- (IBAction)sliderChange:(UISlider *)sender { // When the slider for the power factor is moved
sender.minimumValue = 0.1;
sender.maximumValue = 1.0;
int progress = lround(sender.value);
self.sliderLabel.text = [NSString stringWithFormat:#"%d", progress];
}
Please if any one can help I would much appreciate it.
Two things: your progress variable is an int. Change it to CGFloat.
Next, try using
[NSString stringWithFormat:#"%f", progress]
instead (notice the %f instead of %d). %f is for floating-point values and %d is for integers.
And here's the official [NSString stringWithFormat] formatting docs.

No value displayed in the text field

when i scroll the UISlider, i want to display the current value in UITextField, however i see the following string nan instead of the numeric value of the slider, can you help me please :
- (IBAction)sliderValueChanged:(id)sender
{
nomDeStationTextField.text = [NSString stringWithFormat:#"%.1f", [sender value]];
}
the connections in IB are correct, also, the min value for the slider is 0.0 and the max one is 70, i need to display the current position (i.e : 20) in the UItextField
Cast the sender to UISlider:
nomDeStationTextField.text = [NSString stringWithFormat:#"%.1f", [(UISlider *)sender value]];

Resources