简体   繁体   中英

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];

    [splitField 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?

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.

#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 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]];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM