Working With 4 textfields and id sender 10 buttons - iphone

i`m trying to figure out what if i have 4 textfields field1,field2,field3 and field4, and 10 buttons tagged from 0 to 9, and when i press on a button it will show its tag number on the SELECTED textfield
my code for the 10 buttons is
- (IBAction)buttonPressed:(id)sender {
currentNumber = currentNumber *10 + (float)[sender tag];
field1.text = [NSString stringWithFormat:#"%0.f",currentNumber];
}
this will only send the tag to field1.
i have 4 fields and i want the button to send the tag to the selected field.
is it possible ? if its can you please explain it to me ?
thank you in advance

Have a global
UITextField * selectedField;
When a user selects the field
selectedField = field1
or
selectedField = field2
or
selectedField = field3
or
selectedField = field4
Then you could implement your
- (IBAction)buttonPressed:(id)sender {
currentNumber = [selectedField.text floatValue];
currentNumber += (float)[sender tag];
selectedField.text = [NSString stringWithFormat:#"%0.f",currentNumber];
}
If may be easier (and may hold the right number better) to manipulate your input as a string rather than converting it to the numeric , then adding value, then re-converting back to string though!!! I think you're going to find doing it the way you are, than you will run into floating point conversion problems going back and forth the way you are!!!
This way you are always setting the text to the selected field

i found it
it was very easy :)
if ([field1 isFirstResponder])
that would solve the problem :)

Related

Check if textfield text is almost equal to string

I have a UITextField called textfield. And I have this code to check if the text in the textfield is equal to "exampletext"
if ([textfield.text isEqualToString:#"exampletext"]) {
NSLog(#"Correct");
} else {
NSLog(#"Wrong");
}
But I also want to check if the text in the textfield is almost equal to "exampletext", if the text is almost the same as "exampletext". Like if the text was "eampletex" I want to NSLog(#"Close")
Are there any ways to check if the textfield text is like 50% equal to "exampletext"?
Or any ways to check if the textfield text has 50% the same characters as "exampletext"?
Or something else like that?
What you are looking for is an implementation of the levenshtein distance, levenshtein("hello", "hallo") => 1, levenshtein("hello", "ellos") => 2. You can check this library.
Once you have the distance between the two strings, you could get it as a percentage calculating: percentage = 100 * levenshtein(original,other) / length(original)
Here's my go at it. Create a custom character set from the string you want to match. Check each character in the texfield.text against that character set, and if the number of matches is close to the number of letters in the string, do something..
NSString *testString = #"wordToCompare";
NSString *textFromTextfield = textfield.text;
//create a custom character set from the word you want to compare to...
NSCharacterSet *characterSetForString = [NSCharacterSet characterSetWithCharactersInString:testString];
//keep track of how many matches...
int numberOfCharsThatMatchSet = 0;
for (int x = 0; x < [textFromTextField length]; x++) {
unichar charToCheck = [textFromTextField characterAtIndex:x];
if ([characterSetForString characterIsMember:charToCheck] == YES) {
numberOfCharsThatMatchSet++;
}
NSLog(#"%d", numberOfCharsThatMatchSet);
}
// if the number of matches is the same as the length of the word + or - 2...
if ((numberOfCharsThatMatchSet > [testString length] - 2 ) && (numberOfCharsThatMatchSet < [testString length] + 2 )) {
NSLog(#"close match...");
}
Not sure if this is 100% what you're looking for, but maybe it will help anyway...
I'm sure there might be some open source out there somewhere that would do this for you..however, one approach I can think of that will give you a bit of a lead...
Sort out the characters of both your strings into arrays. Determine which string you want to be the master string and grab the string length of it.
Now compare each character. Ex: Word 1: hello, Word 2: ello.
Each time a letter is found add one to a count. If by the end of your looping your count is 80% of the original length you grabbed from the master string or greater then you most likely have a partial match.
So for our example Word 1 will be our master string and its length is 5. "ello" contains 4/5 characters and therefore is matches 80% of the original string.
I don't think there is an easy way (with several lines of code) of solving this. There are several algorithms you might consider and pick the one which suits your needs most.
You should look at this question. Although it has been designed and answered for another language, you asked for a way or method so you have your solution there.

How to convert from text to number integer?

i have input only number from text field and i want to change it to integer. what code should i write in?
just say text field say for 10 and i want to take that 10 = x, x = integer. so if i use it for mathematic process a = x + 1 and display say 11. is there any chance? i hope you understand what i mean. thank you. i think both of you will answer this simple question but i not because im beginner. im a 3d design graphic switch 180 degree become xcode programming. BTW ive tried this before but it cant.
NSInteger number = [[txtBtn text] integerValue];
Edited :
i've got this error, local declaration of 'number' hides instance variable, pointed to number at result = (number * 2) - 1; and i put the code like this.
NSInteger number = [[txtBtn text] intValue];
result = (number * 2) - 1;
i dont know whats wrong.
Are you sure it's a TextField and not a UIButton you are trying to get the integerValue from?
[txtBtn text]
I created a TextField and a UIButton, and implemented your code:
- (IBAction)btnClicked:(id)sender {
NSInteger i = [[inputField text] intValue];
int result = (i*2)-1;
NSLog(#"%i", result);
}
the output results is correct.
ex: type 5, output in console is 9.

How to make an iOS calculator, by just using a UITextField

I am looking to create an iOS calculator, just by using a UITextField. So for example; somebody may type in '6 - 3 x 2' in the UITextField then hit "Done" or "Go" etc. This would then calculate the answer and display it in a UIAlertView.
So firstly I believe that the UITextField text has to be split up into different sections so '6' '-' '3' '*' '2' would be the separated strings from the UITextField text input. How would I go about separating the text like mentioned above?
How can I then use this information to calculate the answer and to display it?
Use GCMathParser http://apptree.net/parser.htm
It takes an NSString as an input and evaluates it for you.
As Danh said, you can also use DDMathParser https://github.com/davedelong/DDMathParser
An Example(right from the docs)
NSString* mathstring = #"tan(dtor(45))";
double result = [mathstring evaluateMath]; // returns 0.999999999999...
So you simply send message evaluateMath to the NSString you want to parse. That's it)
You could also use NSPredicate as such:
NSPredicate * parsed = [NSPredicate predicateWithFormat:#"22/7.0 + (13*42) - 1024 = 0"];
NSExpression * left = [(NSComparisonPredicate *)parsed leftExpression];
NSNumber * result = [left expressionValueWithObject:nil context:nil];
NSLog(#"result: %#", result);
But I'm not sure about functions like tan and such. More info here.

Store user input, calculate and show in the end

I'm writing a program that calculates input a user gives, simple numbers. 2, 4, 7, 10, 12 and so on.
I need the program to store these inputs (that come in from different textfields) somehow. Use them in an internal equation, and then show them to the user in the end.
How do I do this? Please take note that I'm rather new at programming.
I know how to define textFields, do a calculate IBAction , and I've tried creating a -(void)calculate, which works, but only on 1 input. When I execute the following code, with input from 6 textfields instead of 3 (which are used in the first line (resultOne)) it gives me the wrong numbers. But I still need a better way of doing this.
-(void) calculate{
weightOne = [v1Text.text floatValue]/2;
weightTwo = [v2Text.text floatValue]/2;
resultOne = [m1Text.text floatValue] * weightOne + [s1Text.text floatValue] * weightOne;
resultTwo = [m2Text.text floatValue] * weightTwo + [s2Text.text floatValue] * weightTwo;
_resultTotal = (resultOne + resultTwo) / (weightOne + weightTwo);
NSString *resultString = [[NSString alloc]
initWithFormat: #"%.2f", resultOne];
resultLabel.text = resultString;
[resultString release];
}
I know that the above code outputs resultOne, but this is just for my own testing, to know that the equation works. But I would really like to start all fresh, since I don't believe that the above code is suitable for what I need it to do.
Please bare in mind that I am a beginner, and explain a little more, than just showing me some code.
I look forward to any help you can give me.
EDIT! - BELOW IS NEW CODE
-(IBAction) calculate:(id)sender {
weightOne = 1;
weightTwo = 0.75;
weightThree = 0.50;
mOne = [m1Text.text floatValue];
mTwo = [m2Text.text floatValue];
sOne = [s1Text.text floatValue];
sTwo = [s2Text.text floatValue];
resultOne = (mOne*weightOne) + (sOne*weightOne);
resultTwo = (mTwo*weightTwo) + (sTwo*weightTwo);
_resultTotal = (resultOne + resultTwo) / (weightOne*2 + weightTwo*2);
NSString *resultString = [[NSString alloc]
initWithFormat: #"Gennemsnit %.2f", _resultTotal];
resultLabel.text = resultString;
}
I decided to change my code, in order to easily calculate the input. Now I have another question.
Is it possible to save this equation in some way, so I can reuse it as many times as I want, without having to type the code again and again and again.
First of all calculate can be your IBAction method which will be called when tapped on the button. So change the prototype to -(IBAction)calculate:(id)sender.
Next if you are able to have outlets for the text fields, then there is no harm and absoulutely no difficulty in getting the text entered in them.
Few things you can do are to validate for correct input. [v1Text.text floatValue] is right expression for getting the numbers from text. You can also have a look at intvalue.
Now once you have done your computation, you can use the reference to the label/textfield where you want to display the result.
You can change that label exactly the way you did in your code.

How to subtract the int values within 2 UITextFields

I have a IBAction UILabel to add a number every time it is pressed. I have a Total UITextField that a adds all 3 UILabels. Getting the total seems to be fine until I hit the subtract(buttonTap2) UILabel button.
Here is an example of the IBAction UILabel code not working:
- (IBAction)buttonTap2:(id)sender {
int value = [currentLabel.text intValue] - 1;
currentLabel.text = [NSString stringWithFormat:#"%d",value];
int n1 = [label2.text intValue]; // total labels
int n2 = [label3.text intValue];
int n3 = [label4.text intValue];
int s = n1 - n2 - n3;
NSString *sn = [NSString stringWithFormat:#"%d",s];
[tex7 setText:sn];
What problem are you having? What is "tex7"? If it's supposed to be a textfield or label, is it correctly linked up in Interface Builder? You might have missed it as it isn't very descriptive.
In this case you only get negative values if n1n2>n3. If you want to convert negative numbers to positive number then you take mode of the number ,what ever the number is converted in to Positive number. Another thing you do is that compere the resultant number if it is less then 0 then multiple it with -1,