How to get UISlider to step in decimals - ios

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.

Related

error in iOS calculator decimal point

I built an iOS calculator and I put in a decimal function and whenever I press the decimal button a decimal will be inserted then when i push another number the decimal disappears. I don't know if i need to put code in for the decimal in every number or what. If someone could give me specific instructions it would help me so much.
here is the code for the decimal in .m
- (IBAction)Decimal:(id)sender
{
NSRange range = [self->Screen.text rangeOfString:#"."];
if (range.location ==NSNotFound){
self->Screen.text = [ self->Screen.text stringByAppendingString:#"."];
}
}
and here is .h
-(IBAction)Decimal:(id)sender;
as an example here is one of the number buttons
-(IBAction)Number1:(id)sender{
SelectNumber = SelectNumber * 10;
SelectNumber = SelectNumber + 1;
Screen.text = [NSString stringWithFormat:#"%i", SelectNumber];
update
I fixed some of the errors on my own but am stuck on this one. when I launch the app the screen defaults to zero. whenever I enter any number the zero is supposed to be replaced by the number I select, but it doesn't get replaced. I just don't know exactly what code would point to that issue.
The problem is that by using this sort of line in your number methods:
Screen.text = [NSString stringWithFormat:#"%i", SelectNumber];
You're setting the entirety of Screen (which I'm assuming is a text field or text view of sorts) to contain SelectNumber, which as you say in your comment, is the number selected. Because of this, everything else will be overwritten.
As long as you're not allowing the user to insert text in the middle of your text field/view, you can correct the problem by using the same sort of logic you use in Decimal: and append SelectNumber to the end. Change:
Screen.text = [NSString stringWithFormat:#"%i", SelectNumber];
to:
Screen.text = [Screen.text stringByAppendingString:[NSString stringWithFormat:#"%i", SelectNumber]];

Trying to remove decimal from UISlider and text label

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)];

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

Calculating numbers in UITextField

This may be a very easy question for some of you, but I really would appreciate some help. I am trying to divide one UITextField number by another and when you push the button, it displays it in the third UITextField. Here's what I have so far but get the Invalid operands to binary expression (UITextField *' and 'UITextField *') error.
and yes, i know this is a very simple question, but an answer would really help me. Thanks!
- (IBAction)calculateButton:(id)sender;
{
kdField = firstNum / secondNum;
}
As you can see, I'm trying to set the third UITextField to the firstNum / secondNum when the user presses the calculateButton
Assuming kdField is your UITextField:
kdField.text = [NSString stringWithFormat:#"%d",(firstNum/secondNum)];//or use %f if you're dividing floats
Additionally, if firstNum and secondNum are in UITextFields you'd have to grab the int values with something like:
NSString *firstNumString = firstNumField.text;
int firstNum = [firstNumString intValue];
I Assume KdField, FirstNum and secondNum are all global UITextfield.
Remove the semi-colon after (id)sender
In your buttonAction try something like this:
- (IBAction)calculateButton:(id)sender{
kdField.text = [NSString stringWithFormat:#"%f",[firstNum.text intValue] / [secondNum.text intValue]];
}
I put %f since division can have decimal places

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