2011-08-16 59 views
4

考慮下面的代碼:的UITextView來計算不工作

這裏是我的價格計算器控制器頭文件。

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

這是怎麼回事?

回答

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 
1

您正在嘗試調用的方法float上的每個字段,並沒有方法float

也許你的意思是一樣的東西,而不是[mpgField.text floatValue];

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

您正在使用錯誤的代碼
變化的方法是這樣

[calculator setMpg:[mpgField floatValue]]; 
    [calculator setRiders: [ridersField floatValue]]; 
    [calculator setMiles: [milesField floatValue]]; 
    [calculator setPrice: [priceField floatValue]];