2013-03-11 79 views
0
@implementation ViewController 

-(IBAction)ReturnKeyButton:(id)sender { 
    [sender resignFirstResponder]; 
} 

@synthesize torque, horsepower, rpm, rpmf2; 

-(IBAction)Operation1:(id)sender { 
    float result = 2 * 3.14 *([torque.text floatValue] * [rpm.text floatValue])/33000; 

    answer1.text = [[NSString alloc] initWithFormat:@"%2.f", result]; 
} 

-(IBAction)Operation2:(id)sender { 
    float result = 2 * 3.14 * ([horsepower.text floatValue] * [rpmf2.text floatValue])/ 33000; 

    answer2.text = [[NSString alloc] initWithFormat:@"%2.f", result]; 
} 

我想将我的文本字段格式化为数字。这工作,但它有注意我的植入answer1.text和answer2.text。问题是一个线程点最终会中断。这有什么问题?“”隐藏实例和不完整实现的本地声明

编辑:

@interface ViewController : UIViewController { 
     float result; 
     IBOutlet UILabel *answer1; 
     IBOutlet UILabel *answer2; 
     IBOutlet UITextField *torque; 
     IBOutlet UITextField *rpm; 
     IBOutlet UITextField *horsepower; 
     IBOutlet UITextField *rpmf2; 
     int currentOperation1; 
     float torque1; 
     float rpm1; 
     float horsepower1; 
     float rpm1f2; 
     float answer5; 
    } 

    -(IBAction)Operation1:(id)sender; 
    -(IBAction)Operation2:(id)sender; 
    @property(nonatomic, retain) IBOutlet UITextField *torque; 
    @property(nonatomic, retain) IBOutlet UITextField *rpm; 
    @property(nonatomic, retain) IBOutlet UITextField *horsepower; 
    @property(nonatomic, retain) IBOutlet UITextField *rpmf2; 
    -(IBAction)ReturnKeyButton; 
@end 
+4

我们需要所有的变量声明。 – CodaFi 2013-03-11 12:54:27

+0

警告说什么? – 2013-03-11 12:54:50

回答

5

本地声明隐藏实例变量

第一个错误是因为你有一个成员变量宣布为

float result; 

然后在你的方法中你有一个局部变量e宣布为相同。因此局部变量掩盖了成员变量。你应该确保这些名字不会相互碰撞。

未完全执行

第二个错误是因为你必须声明为在头

-(IBAction)ReturnKeyButton:(id)sender 

的方法,但是,那么你实现

-(IBAction)ReturnKeyButton; 

这是两种完全不同的方法之一是名称为ReturnKeyButton,另一个名称为ReturnKeyButton:注意名称末尾的冒号。

要解决这个问题,只需确保声明符合例如改变

-(IBAction)ReturnKeyButton;-(IBAction)ReturnKeyButton:(id)sender在实施

+0

非常感谢。我一直在为学校而努力,这是我的拳头真正的编码。我一直在盯着这几个小时想想我做错了什么这使得完全感谢你! – 2013-03-11 13:24:52

0

可能是你不调用使用自变量。

尝试调用这样

self.torque.text, self.horsepower.text, self.rpm.text, self.rpmf2.text

希望这有助于!

+0

这将不占用户看到和你的建议不应该有任何影响的警告,因为OP目前只是直接抓住了高德,而无需通过存取去 - 这应该做工精细 – 2013-03-11 13:03:17

1

你的问题包含两个警告信息:

1“”隐藏的实例

您有伊娃的属性,并具有相同名称的方法本地声明。

例如在你的班级中,你有一个名为myProperty的房产,以及你创建myProperty的方法。

一种方法是为您的方法的变量使用不同的名称。或者在综合中覆盖你的财产,其别名为@synthesize myProperty=_myProperty;

#你有float result在课堂上和你的方法。更改结果tempResult或任何其他变量名在这两种方法Operation1:Operation2:

如果您正在使用的XCode 4.4或以上版本,那么编译器synthesize s的_属性。

2.未完全执行 - 需要新鲜

您还没有实现所有您在.h文件已声明的方法。

#你有没有在你的.m文件中实现-(IBAction)ReturnKeyButton;

您已经创建有一个本地方法-(IBAction)ReturnKeyButton:

+0

@interface ViewController中:UIViewController中 { 浮动结果; IBOutlet UILabel * answer1; IBOutlet UILabel * answer2; IBOutlet UITextField *扭矩; IBOutlet UITextField * rpm; IBOutlet UITextField *马力; IBOutlet UITextField * rpmf2; int currentOperation1; float torque1; float rpm1; float horsepower1; float rpm1f2; float answer5; } 我在想什么? – 2013-03-11 13:09:44

+0

- (IBAction)操作1:(id)发送者; (IBAction)操作2:(id)发送者; @property(nonatomic,retain)IBOutlet UITextField *扭矩; @property(nonatomic,retain)IBOutlet UITextField * rpm; @property(nonatomic,retain)IBOutlet UITextField *马力; @property(nonatomic,retain)IBOutlet UITextField * rpmf2; - (IBAction)ReturnKeyButton; @end – 2013-03-11 13:10:00

+0

我缺少什么实现?这是我的.h文件中的所有内容 – 2013-03-11 13:16:24