Iphone &引用;程序接收信号“;SIGABRT“;当建立一个计算器
(我是初学者。) 我在练习导航控制器。我尝试实现一个简单的计算器 我在模拟器中运行了代码。当我按下任何链接到“addFunction”、“substractFunction”、“multiplyFunction”或“divideFunction”的按钮后,它崩溃了 调试器在main.m中标记了以下代码Iphone &引用;程序接收信号“;SIGABRT“;当建立一个计算器,iphone,objective-c,ios,xcode,Iphone,Objective C,Ios,Xcode,(我是初学者。) 我在练习导航控制器。我尝试实现一个简单的计算器 我在模拟器中运行了代码。当我按下任何链接到“addFunction”、“substractFunction”、“multiplyFunction”或“divideFunction”的按钮后,它崩溃了 调试器在main.m中标记了以下代码 int retVal = UIApplicationMain(argc, argv, nil, nil); 并说“线程1:程序收到信号:“SIGABRT” 有人知道如何应对这种情况吗?谢谢 代码
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
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
看来你的钮扣不是钮扣。它们似乎是我们的看法。从您的nib中删除按钮并将新按钮放在那里,然后将它们链接到我们的IBActions。从上面的例外情况来看,您的IBActions似乎没有正确连接。 正如dasdom提到的,删除所有按钮,创建新按钮,然后相应地添加iAction方法 还有一件事我在你的代码中认识到,在乘法和除法中有一个内存泄漏
resultLabel.text = [[NSString alloc] initWithFormat:@"%2.f" , a*b];
应该是
resultLabel.text = [[[NSString alloc] initWithFormat:@"%2.f" , a*b]autorelease];
或
在划分方法上也做了类似的改变
您将背景点击方法链接到了什么?您是否为所有按钮设置了插座?在Interface Builder中检查它。将断点设置为iAction方法之一,并让我知道确切的行。2011-08-20 19:57:48.426更改[28442:207]-[UIView addTarget:action:forControlEvents:]:发送到实例0x4e27870 2011-08-20 19:57:48.430更改[28442:207]的无法识别的选择器***由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:'-[UIView addTarget:action:forControlEvents:]:未识别的选择器发送到IB中的实例0x4e27870',它是否显示任何警告?我在Interface Builder中未看到任何警告。如果它们实际上是按钮,请通过第三个检查器在IB中更改它们的类(Command-Option-3)我已经检查了Identity Inspector。每个按钮都属于“UIButton”类。您是如何设置连接的。Control单击按钮,然后将一行拖动到.h文件中?单击按钮。Ctrl单击“内部润色”,然后将一行拖动到文件所有者并选择方法。
#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
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];