2013-03-11 78 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