UITextView to Calculation Not Working
Consider the following code:
Here is my price calculator controller header file.
#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
Here is its implementation file:
#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];
[sp开发者_运维百科litField setFloatValue:split];
}
@end
It's giving me the error:
receiver type 'UITextField' for instance message does not
declare a method with selector 'float' [4]
What's going on?
this may help you: http://www.iphonedevsdk.com/forum/iphone-sdk-development/87944-uitextview-calculation-not-working.html
#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
You are trying to call the method float
on each of your fields, and there IS no method float
Perhaps you meant something like [mpgField.text floatValue];
instead.
You are using wrong code
change method like this
[calculator setMpg:[mpgField floatValue]];
[calculator setRiders: [ridersField floatValue]];
[calculator setMiles: [milesField floatValue]];
[calculator setPrice: [priceField floatValue]];
You need to for a start define all of your IBOulets as @property(nonretain, atomic) ... and then synthesise them.
You will then need to call
[[fieldname text] floatValue]
on the fields.
EDIT:
Here's the code you want:
[calculator setMpg:[[mpgField text] floatValue]];
[calculator setRiders: [[ridersField text] floatValue]];
[calculator setMiles: [[milesField text] floatValue]];
[calculator setPrice: [[priceField text] floatValue]];
精彩评论