当我调用此方法时,它返回0

时间:2013-09-04 20:38:46

标签: macos cocoa

逻辑是:从视图中读取数据(3个文本字段),然后按下按钮时,将存储在模型中的数据汇总并打印出来。

< - 头文件 - >

    #import <Cocoa/Cocoa.h>

@class QuoteAttributes;

@interface AppDelegate : NSObject <NSApplicationDelegate>

@property (assign) IBOutlet NSWindow *window;
@property (weak) IBOutlet NSTextField *amountOfHardware;
@property (weak) IBOutlet NSTextField *amountOfPrinter;
@property (weak) IBOutlet NSTextField *amountOfSoftware;
@property (strong) QuoteAttributes *quickQuote;


- (IBAction)calculate:(id)sender;
- (void) setValueTotheForm;

@end

请参阅以下代码:

#import "AppDelegate.h"
#import "QuoteAttributes.h"

@implementation AppDelegate

@synthesize amountOfHardware;
@synthesize amountOfPrinter;
@synthesize amountOfSoftware;

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
    // Insert code here to initialize your application
}

- (void) setValueTotheForm{
    [self.quickQuote setNumHardware:[self.amountOfSoftware intValue]];
    [self.quickQuote setNumPrinter:[self.amountOfPrinter intValue]];
    [self.quickQuote setNumSoftware:[self.amountOfSoftware intValue]];

    int totalQuote = [self.quickQuote numHardware] + [self.quickQuote numPrinter] + [self.quickQuote numSoftware];

    NSLog(@"%d", totalQuote);
}

- (IBAction)calculate:(id)sender {
    [self setValueTotheForm];
}
@end

但是,当我调用此方法时,它会打印0

1 个答案:

答案 0 :(得分:0)

应首先启动对象:

    - (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
    // Insert code here to initialize your application
    QuoteAttributes *qa = [[QuoteAttributes alloc] init];
    [self setQuickQuote: qa];
}

然后上述代码将起作用。