请考虑以下代码:
这是我的价格计算器控制器头文件。
#import <Foundation/Foundation.h>
#import "PriceCalculator.h"
@interface PriceCalculatorController : UITextField {
IBOutlet UITextField *mpgField;
IBOutlet UITextField *milesField;
IBOutlet UITextField *priceField;
IBOutlet UITextField *ridersField;
IBOutlet UITextField *splitField;
PriceCalculator *calculator;
}
-(IBAction)calculator:(id)sender;
@end
以下是其实施文件:
#import "PriceCalculatorController.h"
@implementation PriceCalculatorController
- (IBAction)calculator:(id)sender {
float split;
calculator = [[PriceCalculator alloc]init];
[calculator setMpg:[mpgField float]];
[calculator setRiders: [ridersField float]];
[calculator setMiles: [milesField float]];
[calculator setPrice: [priceField float]];
split = [calculator CalculateSplit];
[splitField setFloatValue:split];
}
@end
它给了我错误:
receiver type 'UITextField' for instance message does not
declare a method with selector 'float' [4]
发生了什么事?
答案 0 :(得分:2)
答案 1 :(得分:1)
您尝试在每个字段上调用方法float
,并且没有方法float
也许你的意思是[mpgField.text floatValue];
而已。
答案 2 :(得分:1)
#import "PriceCalculatorController.h"
@implementation PriceCalculatorController
- (IBAction)calculator:(id)sender {
float split;
calculator = [[PriceCalculator alloc]init];
[calculator setMpg:[mpgField.text floatValue]];
[calculator setRiders: [ridersField.text floatValue]];
[calculator setMiles: [milesField.text floatValue]];
[calculator setPrice: [priceField.text floatValue]];
split = [calculator CalculateSplit];
[splitField setFloatValue:split];
}
@end
答案 3 :(得分:1)
您使用的代码错误 像这样改变方法
[calculator setMpg:[mpgField floatValue]];
[calculator setRiders: [ridersField floatValue]];
[calculator setMiles: [milesField floatValue]];
[calculator setPrice: [priceField floatValue]];
答案 4 :(得分:0)
您需要首先将所有IBOulets定义为@property(nonretain,atomic)...然后合成它们。
然后您需要致电
[[fieldname text] floatValue]
在田野上。
编辑:
这是您想要的代码:
[calculator setMpg:[[mpgField text] floatValue]];
[calculator setRiders: [[ridersField text] floatValue]];
[calculator setMiles: [[milesField text] floatValue]];
[calculator setPrice: [[priceField text] floatValue]];