我创建了一种计算简单计算的方法。
我希望该方法返回一个 int 并采用 2 个 int 参数。
我创建了一个名为计算的类。我将计算导入 viewController.m 并创建了一个动作 2 textFields 和一个文本标签。我正在使用此视图来测试该方法。我的方法在计算中返回 0。我究竟做错了什么?这很简单,但我似乎无法弄清楚我在哪里犯了错误。这是代码。
计算.h
#import <Foundation/Foundation.h>
@interface Calculation : NSObject
@property (nonatomic) int odometerStart;
@property (nonatomic) int odometerEnd;
@property (nonatomic) int odometerTotal;
- (int)mileageStart:(int)start mileageEnd: (int)end;
@end
计算.m
#import "Calculation.h"
@implementation Calculation
@synthesize odometerEnd, odometerStart, odometerTotal;
- (int)mileageStart:(int)start mileageEnd:(int)end
{
odometerStart = start;
odometerEnd = end;
odometerTotal = end - start;
return odometerTotal;
}
@end
视图控制器.h
#import <UIKit/UIKit.h>
@interface MileageViewController : UIViewController
- (IBAction)calculateMileage:(id)sender;
@property (strong, nonatomic) IBOutlet UITextField *startLabel;
@property (strong, nonatomic) IBOutlet UITextField *endLabel;
@property (strong, nonatomic) IBOutlet UILabel *displayLabel;
@end
视图控制器.m
#import "MileageViewController.h"
#import "Calculation.h"
@implementation MileageViewController
@synthesize startLabel;
@synthesize endLabel;
@synthesize displayLabel;
- (void)viewDidUnload
{
[self setStartLabel:nil];
[self setEndLabel:nil];
[self setDisplayLabel:nil];
[super viewDidUnload];
// Release any retained subviews of the main view.
}
- (IBAction)calculateMileage:(id)sender {
Calculation *mileage = [[Calculation alloc] init];
int odomStart = [[startLabel text] intValue];
int odomEnd = [[endLabel text ] intValue];
[mileage mileageStart:odomStart mileageEnd:odomEnd];
mileage.odometerTotal = displayLabel.text.intValue;
NSLog(@"THe total is %d", mileage.odometerTotal);
我的总数一直等于 0。计算没有被计算。