2011-05-11 36 views
0

我已經寫了溫度轉換爲iPhone代碼,但它無法正常工作,並沒有給出任何錯誤,請幫助溫度轉換不給輸出

接口部分

#import <UIKit/UIKit.h> 

float n ; 
float k; 



@interface farh_celcius_conv_AppDelegate : NSObject <UIApplicationDelegate> { 
    UIWindow *window; 
    UILabel *display; 
    IBOutlet UITextField *farhenite; 
    IBOutlet UIButton *convert; 

} 


-(IBAction) convert; 


@property (nonatomic, retain) IBOutlet UIWindow *window; 
@property (nonatomic, retain) IBOutlet UILabel *display; 
@property (nonatomic, retain) IBOutlet UITextField *farhenite; 
@property (nonatomic, retain) IBOutlet UIButton *convert; 
@end 

實現部分

#import "farh_celcius_conv_AppDelegate.h" 


@implementation farh_celcius_conv_AppDelegate 


@synthesize window,display,farhenite; 





-(IBAction) convert { 

    NSString *str = [NSString text]; 

    float n = [str floatValue]; 


     k = (n - 32)*(5/9); 



    [display setText:[NSString stringWithFormat:@"%f",k]]; 

} 

- (void)applicationDidFinishLaunching:(UIApplication *)application {  

    // Override point for customization after application launch 
    [window makeKeyAndVisible]; 
} 


- (void)dealloc { 
    [window release]; 
    [super dealloc]; 
} 


@end 
+0

請格式化你的代碼部分,以便它們正確顯示! – jv42 2011-05-11 09:43:20

+0

什麼是我在[NSString我],我沒有申報任何地方。 – 2011-05-11 09:45:22

+0

NSString * str = [NSString i];什麼是'我'? – PgmFreek 2011-05-11 09:46:58

回答

1

如果你想在文本框輸入的數值轉換成你的方法應該是這樣的

-(IBAction) convert { 

    NSString *str = yourTextField.text; // In your case farhenite i guess 

    float n = [str floatValue]; 


     k = (n - 32)*(5/9); 



    [display setText:[NSString stringWithFormat:@"%f",k]]; 

} 
+0

@ 7kv7 NSString * str = yourTextField.text;這行代碼執行什麼? – 2011-05-11 10:09:56

+0

其用於從您擁有的文本框中獲取文本。在你的情況下,它應該是你的textfield的名稱,我認爲是farhenite – visakh7 2011-05-11 10:11:39

+0

@ 7kv7現在,如果我在模擬器中輸入一個值,它不會進入* str並且n的值保持爲0 – 2011-05-11 10:53:11

0
NSString *str = display.text; 

只需更換str聲明即可。

1

,如果你婉從文本框轉換,那麼它應該是

NSString *str = textField.text; 
1

替換爲你的函數低於功能。 比它會正常工作。

- (IBAction爲)將{

NSString *str = display.text; 

float n = [str floatValue]; 


    k = (n - 32)*(5/9); 



[display setText:[NSString stringWithFormat:@"%f",k]]; 

}

1

如果你想從farhenite文本字段轉換價值,那麼它應該是

-(IBAction) convert { 

    NSString *str = farhenite.text; 

    float n = [str floatValue]; 


     k = (n - 32)*(5/9); 



    [display setText:[NSString stringWithFormat:@"%f",k]]; 

} 
+0

@ iphone pgr現在,如果我在模擬器中輸入一個值,它不會進入* str並且n的值保持爲0 – 2011-05-11 10:43:19

+0

您是否連接了插座? – PgmFreek 2011-05-11 11:34:56

+0

@ iphonepgr是的,我連接iboutlet – 2011-05-11 11:36:20