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]];
Related
I'm new to Xcode and Objective-C. I watched a tutorial where he compared a char with the entered text in the textfield of the simulator. He used the following code:
BOOL isUsersEqual = [self.username isEqualToString:[self.usernameTextField text]];
When trying to build a small app myself I have problems to get the number out of the Textfield and be processed into my code. I want it to compare the random number I generated to the number in the textfield. How can I accomplish that?
I know this is kind of a nooby question and I don't wanna ask every time. Is there someplace I can look up all those types of questions?
You can try it by converting the number into string and then comparing -
NSString *number = [self.username stringValue];
if(number isEqualToString:self.usernameTextField.text){
//Do Something
}
else{
//Do Something
}
But this method only works when you're comparing Equals To -
For Greater than/ Less than/Equal to you can try it by converting the textfield's text into number and then comparing -
NSNumber *number = [self.usernameTextField.text integerValue];
if(number == self.username){
//Do Something
}
else{
//Do Something
}
To compare your generated number with a number from textField, you must first typecast the textField text to NSInteger using
NSInteger b = [[textField text] integerValue];
then you can proceed to compare the 2 numbers.
To fully understand these types of questions, I recommend you learning about objective-c primitive types and typecasting. Good luck!
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]];
}
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)];
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.
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