我已为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 :(得分:1)
如果你想转换在textField中输入的值,那么方法应该是这样的
-(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]];
}
答案 1 :(得分:1)
如果你想从文本字段转换,那么它应该是
NSString *str = textField.text;
答案 2 :(得分:1)
用以下功能替换您的功能。 比它能正常工作。
- (IBAction)转换{
NSString *str = display.text;
float n = [str floatValue];
k = (n - 32)*(5/9);
[display setText:[NSString stringWithFormat:@"%f",k]];
}
答案 3 :(得分:1)
如果您想转换来自farhenite文本字段的值,那么它应该是
-(IBAction) convert {
NSString *str = farhenite.text;
float n = [str floatValue];
k = (n - 32)*(5/9);
[display setText:[NSString stringWithFormat:@"%f",k]];
}
答案 4 :(得分:0)
NSString *str = display.text;
只需替换str声明。