2012-07-25 75 views
1

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

到目前为止,这是我的代码。它是在文本字段中写入的,事件编号为。林相当肯定这是错的,但我找不到替代解决方案。此外,我还没有得到实时的工作,所以我有点恢复到按下文本字段时完成以下步骤。

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

好的,我对Objective-C一无所知,所以我怀疑我可以帮助解决您的问题。但是我会在这里提出问题来帮助你:C和C++是不同的语言,所以你不应该用两者来标记你的问题。但是这个问题实际上是关于*另一种不同的语言*:Objective-C。我这次为你解决了标签问题。欢迎来到Stack Overflow。 – 2012-07-25 04:38:21

回答

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 

希望这有助于!

+0

现在就试试这个:)谢谢你的回答。我一定会回来,让你知道会发生什么 – 2012-07-25 05:35:20

+0

你的答案的第二部分是什么意思。 @interface yourClassName:yourSuperclass {} 该部分是否意味着在我的ViewController.h中写入代码? – 2012-07-25 05:53:13

+0

我已经在没有错误的正确位置添加了所有代码,并且有一些调整:)我现在运行该应用程序,并且插入了例如10 + 3的和,没有任何反应,没有任何内容出现在Answer_TextField中。 非常抱歉,我的noobieness,但我将不得不添加更多的代码,像大东西缺少将信息发送到Answer_TextField – 2012-07-25 06:18:43