«Программа получила сигнал« SIGABRT »при построении калькулятора - PullRequest
2 голосов
/ 20 августа 2011

(я новичок.) Я занимаюсь навигационным контроллером.Я пытаюсь реализовать простой калькулятор.

Я запустил код в симуляторе.После того, как я нажал любую кнопку, которая была связана с «addFunction», «substractFunction», «multiplyFunction» или «DivineFunction», произошел сбой.

Отладчик отметил следующий код в 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

Ответы [ 2 ]

1 голос
/ 23 августа 2011

Из вышеприведенного исключения кажется, что ваши IBActions не подключены должным образом.Как уже упоминалось, удалите все свои кнопки, создайте новые кнопки и затем добавьте соответствующие методы 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];

и также выполнить аналогичное изменение в методе деления.

С чем вы связали свой метод backgroundtap?

0 голосов
/ 20 августа 2011

Кажется, что вы кнопки не кнопки.Они, кажется, вид.Удалите кнопки из своего пера, вставьте новые кнопки и свяжите их с нашими IBActions.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...