2011-08-20 129 views
2

(我是初學者)。 我練導航控制器。我試圖實現一個簡單的計算器。「計劃接收信號‘SIGABRT’

我跑在模擬器中的代碼。之後,我壓將其鏈接到 「調用addFunction」, 「substractFunction」, 「multiplyFunction」 或 「divideFunction」 的任何按鈕,它墜毀。

調試器標記的main.m

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

下面的代碼並表示「主題1:編程接收信號:「SIGABR T「。」

有沒有人知道如何應對這種情況?謝謝。

下面的代碼:

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 
+1

您是否爲所有按鈕設置了Outlet?在Interface Builder中檢查它。 –

+0

是的,我已經完成了。 – lavitanien

+0

將斷點設置爲IBAction方法之一,並讓我知道確切的線。 –

回答

0

看來你的按鈕不是按鈕。他們似乎是看法。從你的筆尖刪除按鈕,並在那裏放置新的按鈕,並將它們鏈接到我們的IBActions。

+0

如果它們實際上是按鈕,則通過第三個檢查器(Command-Option-3)在IB中更改它們的類。 – FeifanZ

+0

我已經檢查了Identity Inspector。每個按鈕都屬於「UIButton」類。 – lavitanien

+0

你是如何設置連接的。控制單擊按鈕,然後將一行拖入.h文件中? – dasdom

1

從上面的例外看來,您的IBA似乎沒有正確連接。 正如dasdom提到的那樣,刪除所有按鈕,創建新按鈕,然後相應地添加IBAction方法。

還多了一個我在你的代碼的認可,在乘除法的事情有自己編寫leak.You內存

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方法?