实时计算器

时间:2012-07-25 04:36:15

标签: objective-c ios

对于学校作业,我被告知要制作一个计算器应用程序,就像聚光灯计算器一样。它实时工作,没有按钮可以开始。

到目前为止,这是我的代码。它写在文本字段中,其中包含事件编辑已结束。我很确定这是错的,但我找不到另一种解决方案。此外,我没有得到实时的工作,所以我有点恢复到按下文本字段时完成以下步骤。

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

1 个答案:

答案 0 :(得分:2)

我认为更好的方法是实现像textViewDidChange:这样的UITextViewDelegate协议方法。例如,你可以这样做:

- (void)textViewDidChange:(UITextView *)textView {

    NSString *currentText = [textview text];
    NSArray *currentItems = [currentText componenetsSeparatedByString:@" "];
    float result = 0.0;

    //If a valid expression is in the text view
    if([currentItems count] > 2) {

        float num1 = [[currentItems objectAtIndex:0] floatValue];
        float num2 = [[currentItems objectAtIndex:2] floatValue];
        NSString *operator = [currentItems objectAtIndex:1];

        if([operator isEqualToString:@"+"]) {

            result = num1 + num2;
            answerTextField.text = [NSString stringWithFormat:@"%f", result];
        }
        else if([operator isEqualToString:@"-"]) {

            result = num1 - num2;
            answerTextField.text = [NSString stringWithFormat:@"%f", result];
        }
        else if([operator isEqualToString:@"*"]) {

            result = num1 * num2;
            answerTextField.text = [NSString stringWithFormat:@"%f", result];
        }
        else if([operator isEqualToString:@"/"]) {

            result = num1 / num2;
            answerTextField.text = [NSString stringWithFormat:@"%f", result];            
        }
        else{

            answerTextField.text = @"Invalid Operation";
        }
    }
} 

每次用户在文本视图中编辑文本时都会调用此方法。它应该工作,但我没有测试它。确保在此代码所在的任何文件的标题中,您执行以下操作:

@interface yourClassName : yourSuperclass <UITextViewDelegate> {

    //Your instance variables
}

//Your method and property declarations

修改

假设我将- (void)textViewDidChange:(UITextView *)textView代码放在名为MyClass.m的文件中。文件MyClass.m将如下所示:

@implementation MyClass

- (void)textViewDidChange:(UITextView *)textView {

    //All the above code goes here
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    //INCLUDE THESE LINES
    Sum_TextField.delegate = self;
    Answer_TextField.delegate = self;
}

- (void)viewDidUnload
{
    [super viewDidUnload];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return YES;
}

@end

在头文件(MyClass.h)中,我会把它放在:

@interface MyClass : UIViewController <UITextViewDelegate> 

//Note: you don't declare - (void)textViewDidChange:(UITextView *)textView in the header file because you are implementing
//a protocol method.

//MAKE SURE SUM_TEXTFIELD AND ANSWER_TEXTFIELD ARE UITEXTVIEWS NOT UITEXTFIELDS
@property (strong, nonatomic) IBOutlet UITextView *Sum_TextField;
@property (strong, nonatomic) IBOutlet UITextView *Answer_TextField;

@end

希望这有帮助!