-4

逻辑是:从视图(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

4

1 回答 1

0

应该首先启动对象:

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

那么上述代码将起作用。

于 2013-09-04T21:55:02.140 回答