iphone - "Program received signal "SIGABRT”构建计算器时

标签 iphone objective-c ios xcode

(我是初学者。) 我正在练习导航 Controller 。我尝试实现一个简单的计算器。

我在模拟器中运行代码。在我按下链接到“addFunction”、“substractFunction”、“multiplyFunction”或“divideFunction”的任何按钮后,它崩溃了。

调试器在main.m中标记了如下代码

int retVal = UIApplicationMain(argc, argv, nil, nil);

并说“线程 1:程序接收到信号:“SIGABRT”。”

有谁知道如何处理这种情况?谢谢。

代码如下:

ChangeAppView.h:

#import <UIKit/UIKit.h>
@class ChangeViewController;
@interface ChangeAppDelegate : NSObject <UIApplicationDelegate>
{
    UIWindow *window;
    UINavigationController *navigationController;
}

@property (nonatomic, retain) IBOutlet UIWindow *window;
@property (nonatomic, retain) UINavigationController *navigationController;
@end

ChangeAppDelegate.m:

#import "ChangeAppDelegate.h"
#import "ChangeViewController.h"

@implementation ChangeAppDelegate
@synthesize window;
@synthesize navigationController;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    navigationController = [[UINavigationController alloc] init]; 
    self.window.rootViewController = navigationController; 
    ChangeViewController *changeViewController = [[ChangeViewController alloc] initWithNibName:@"ChangeViewController" bundle:nil]; 
    [navigationController pushViewController:changeViewController animated:YES];
    [changeViewController release];
    [self.window makeKeyAndVisible]; 
    return YES;
}

- (void)dealloc
{
    [navigationController release];
    [window release];
    [super dealloc];
}
@end

CalculatorViewController.h:

#import <UIKit/UIKit.h>
@interface CalculatorViewController : UIViewController 
{
    IBOutlet UITextField *numberField1;
    IBOutlet UITextField *numberField2;
    IBOutlet UILabel *resultLabel;
}
@property (nonatomic , retain) IBOutlet UITextField *numberField1;
@property (nonatomic , retain) IBOutlet UITextField *numberField2;
@property (nonatomic , retain) IBOutlet UILabel *resultLabel;
-(IBAction)addFunction:(id)sender;
-(IBAction)substractFunction:(id)sender;
-(IBAction)multiplyFunction:(id)sender;
-(IBAction)divideFunction:(id)sender;
-(IBAction)clear:(id)sender;
-(IBAction)backgroundTap:(id)sender;
@end

CalculatorViewController.m:

#import "CalculatorViewController.h"
@implementation CalculatorViewController
@synthesize numberField1;
@synthesize numberField2;
@synthesize resultLabel;
-(IBAction)addFunction:(id)sender
{
    float a = ([numberField1.text floatValue]);
    float b = ([numberField2.text floatValue]);    
    resultLabel.text = [NSString stringWithFormat:@"%2.f" , a+b];
}

-(IBAction)substractFunction:(id)sender
{
    float a = ([numberField1.text floatValue]);
    float b = ([numberField2.text floatValue]);
    NSString *result = [[NSString alloc] initWithFormat:@"%2.f" , a-b];
    resultLabel.text = result;
    [result release];
}

-(IBAction)multiplyFunction:(id)sender
{
    float a = ([numberField1.text floatValue]);
    float b = ([numberField2.text floatValue]);
    resultLabel.text = [[NSString alloc] initWithFormat:@"%2.f" , a*b];
}

-(IBAction)divideFunction:(id)sender
{
    float a = ([numberField1.text floatValue]);
    float b = ([numberField2.text floatValue]);
    resultLabel.text = [[NSString alloc] initWithFormat:@"%2.3f" , a/b];
}

-(IBAction)clear:(id)sender
{
    numberField1.text = @"";
    numberField2.text = @"";
    resultLabel.text = @"";
}

-(IBAction)backgroundTap:(id)sender
{
    [numberField1 resignFirstResponder];
    [numberField2 resignFirstResponder];
}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
    // Custom initialization
    }
    return self;
}

- (void)dealloc
{
    [numberField1 release];
    [numberField2 release];
    [resultLabel release];
    [super dealloc];
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];  
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle
- (void)viewDidLoad
{
    self.title = @"Calculator";
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
@end

最佳答案

从上面的异常看来,您的 IBActions 似乎没有正确连接。 正如 dasdom 提到的,删除所有按钮,创建新按钮,然后相应地添加 IBAction 方法。

还有一件事我在你的代码中认识到,在乘法和除法中存在内存泄漏。你已经写了

resultLabel.text = [[NSString alloc] initWithFormat:@"%2.f" , a*b];

应该是

resultLabel.text = [[[NSString alloc] initWithFormat:@"%2.f" , a*b]autorelease]; 

resultLabel.text = [NSString StringWithFormat:@"%2.f" , a*b];

并且在 divide 方法中也做类似的改变。

您将 backgroundtap 方法链接到什么?

关于iphone - "Program received signal "SIGABRT”构建计算器时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7131463/

相关文章:

iphone - UIImagePickerController 在快速滚动时崩溃,比照片应用程序慢

Objective-C 的 "obj performSelector"vs objc_msgSend( )?

objective-c - TextView 处于焦点时隐藏 iPad 键盘的 SDK 安全方法

ios - 如何确定节点在 sprite kit 中出现的顺序

ios - 当另一个单元格按钮被选中时,将之前的单元格按钮图像设置为默认状态

iphone - 将数据从委托(delegate)传递到 UIViewController

iphone - 是否可以使用 Accelerate/LAPACK 求解非方欠/过约束矩阵?

ios - 从其他应用程序和 NotificationCenter 接收有意义的通知

iphone - 比较字符串 Objective C

ios - 为什么UIToolbar和UITabBar中的项目属性是(复制)属性,而不是(保留)属性?