Calculating numbers in UITextField - ios

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

Related

Is that integer equal to the number in textfield

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!

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;

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

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 use UITextFields?

I'm doing the following in my App right now:
NSInteger myReell1 = 6;
NSInteger myReell2 = 7;
NSInteger myImag1 = -5;
NSInteger myImag2 = 3
+ (WSData *)scientificData
{
float dataSet2D[2] = {0,myReell1 + myReell2};
float dataSet3D[2] = {0,myImag1 + myImag2};
return [WSData dataWithValues:[WSData arrayWithFloat:dataSet2D len:2]
valuesX:[WSData arrayWithFloat:dataSet3D len:2]];
}
This worked fine for me up to now.
Here is what I would like to change and what I would like to ask you for:
I would like to use 4 UITextFields to give the opportunity of personal input to the user, instead of the consistent Integers (myReell1, … ,myImag2) which I use right now.
How would you implement those UITextfields (maybe just 1 example?) into the code above to make the NSInteger part needless.
Please don't mind about the actual stuff inside of the scientific data. ;-)
Presuming this code is inside a view controller, you can:
Create your UITextField member variables in the class.
Subscribe the class to UITextFieldDelegate.
Wire your text fields up in Interface Builder (or instantiate them programatically and add them to the view).
Implement the didFinishEditing delegate method to handle the
actual input.
Say you make 4 textfield boxes, you can get the integer from going
myReell1 = [textFieldOne.text intValue];
and so on, there you have an integer you can use constantly, with no hardcoding
First put Textfield and then when user click some button just put bellow code in you button click metho...
NSInteger myReell1 = [textField1.text intValue];
NSInteger myReell2 = [textField2.text intValue];
NSInteger myImag1 = [textField3.text intValue];
NSInteger myImag2 = [textField4.text intValue];
:)

Resources