我是 iphone 应用程序世界的新手。所以我想我用计算器应用试试运气。不幸的是,我遇到了一个问题,如果我在计算器中按下第三个键,应用程序就会崩溃。有时我会收到此错误 EXC_BAD_ACCESS。这是我的 CalculatorViewController.m 文件中的代码。
#import "CalculatorViewController.h"
@implementation CalculatorViewController
@synthesize screenText;
- (IBAction)buttonPressed:(id)sender {
NSString *title = [sender titleForState:UIControlStateNormal];
[self collect:title];
}
- (void)collect:(NSString *)digitz {
NSString * newText = nil;
if ([digitz isEqualToString:@"+"]) {
[self add:result];
big_digit = nil;
}
else if ([digitz isEqualToString:@"+"]) {
[self sub:result];
}
else if ([digitz isEqualToString:@"x"]) {
[self multiply:result];
}
else if ([digitz isEqualToString:@"="]) {
[self equate:result];
}
else {
if (big_digit != nil && [big_digit isEqualToString:@"0"] == FALSE)
big_digit = [big_digit stringByAppendingFormat:@"%@",digitz];
else
big_digit = (NSMutableString *) digitz;
result = (int) big_digit;
newText = [[NSString alloc] initWithFormat:
@"%@",big_digit];
}
screenText.text = newText;
[newText release];
}
- (void)add:(int)res {
NSString * newText = nil;
ans = ans + res;
newText = [[NSString alloc] initWithFormat:
@"%@",ans];
screenText.text = newText;
[newText release];
}
任何人都可以在这里发现一个明显的问题。这也是相应的头文件。
#import <UIKit/UIKit.h>
@interface CalculatorViewController : UIViewController {
UILabel *screenText;
int number;
int result;
int ans;
//NSString *big_digit;
NSMutableString * big_digit ;
}
@property (nonatomic, retain) IBOutlet UILabel *screenText;
- (IBAction)buttonPressed:(id)sender;
- (void)collect:(NSString *)digitz;
- (void)add:(int)num;
- (void)sub:(int)num;
- (void)multiply:(int)num;
- (void)equate:(int)num;
@end