For a school assignment I have been told to make a calculator app, the same as the spotlight calculator. It works in realtime and has no buttons for things to begin.
So far this is my code. It is written in a text field with the event Editing Did End. Im pretty sure thats wrong but i can’t find an alternative solution. Also i haven’t gotten the realtime thing to work so i’ve kind of reverted to completing the following steps when pressed off the text field.
- (IBAction)Didend_Action:(id)sender {
NSString *list = [Sum_TextField text];
NSArray *listItemsArray = [list componentsSeparatedByString:@" "];
float firstNumber = [[listItemsArray objectAtIndex: 0] floatValue];
NSString *symbol = [listItemsArray objectAtIndex: 1];
float secondNumber = [[listItemsArray objectAtIndex: 2] floatValue];
{
Calculator* calc = [[Calculator alloc] init];
[calc setNum1:firstNumber];
[calc setNum2:secondNumber];
if ([symbol isEqualToString:@"-"])
{
[calc minus];
}
else if ([symbol isEqualToString:@"+"])
{
[calc add];
}
if ([symbol isEqualToString:@"*"])
{
[calc multiply];
}
else if ([symbol isEqualToString:@"/"])
{
[calc divide];
}
[Answer_TextField setText:[NSString stringWithFormat:@"%d", [calc answer]]];
}
}
I think a better way to do it would be to implement the UITextViewDelegate protocol methods like
textViewDidChange:. For example, you could do something like this:This would be called every time the user edited the text in the text view. It should work, but I didn’t test it out. Make sure that in the header of whatever file this code is in, you do this:
EDIT:
Let’s say I put the
- (void)textViewDidChange:(UITextView *)textViewcode in a file called MyClass.m. The file MyClass.m would then look like this:In the header file (MyClass.h), I would put this:
Hope this helps!