将极其基本的objective-c程序连接到iPhone应用程序
我想知道我是否可以将objective-c中的这个简单控制台程序连接到一个非常简单的iPhone应用程序中 由于我没有使用Interface Builder的经验,我不确定学习它需要多长时间 此外,我相信我的代码必须根据某些iPhone API进行调整,而不是使用控制台进行输入和输出 以下是程序的主.m文件,其中存储了程序的所有代码:将极其基本的objective-c程序连接到iPhone应用程序,iphone,c,objective-c,ios4,Iphone,C,Objective C,Ios4,我想知道我是否可以将objective-c中的这个简单控制台程序连接到一个非常简单的iPhone应用程序中 由于我没有使用Interface Builder的经验,我不确定学习它需要多长时间 此外,我相信我的代码必须根据某些iPhone API进行调整,而不是使用控制台进行输入和输出 以下是程序的主.m文件,其中存储了程序的所有代码: //Simple program to convert Fahrenheit to Celsius and Celsius to Fahrenheit #imp
//Simple program to convert Fahrenheit to Celsius and Celsius to Fahrenheit
#import <stdio.h>
@interface Converter: NSObject
{
//Instance variable which stores converting formula for objects
double formula;
}
//Declare instance methods for setting and getting instance variables
-(void) convert: (double) expression;
-(double) formula;
@end
@implementation Converter;
//Define instance method to set the argument (expression) equal to the instance variable
-(void) convert: (double) expression
{
formula = expression;
}
//Define instance method for returning instance variable, formula
-(double) formula
{
return formula;
}
@end
int main (int argc, char *argv[])
{
//Point two new objects, one for the Fahrenheit conversions and the other for the Celsius conversions, to the Converter class
Converter *fahrenheitConversion = [[Converter alloc] init];
Converter *celsiusConversion = [[Converter alloc] init];
//Declare two double variables holding the user-inputted data, and one integer variable for the if else statement
double fahrenheit, celsius;
int prompt;
NSLog(@"Please press 0 to convert Celsius to Fahrenheit, or 1 to convert Fahrenheit to Celsius\n ");
scanf("%d", &prompt);
if(prompt == 0) {
NSLog(@"Please enter a temperature in Celsius to be converted into Fahrenheit!:\n");
scanf("%lf", &celsius);
if(celsius < -273.15) {
NSLog(@"It is impossible to convert temperatures less than −273.15 degrees Celsius, because this is absolute zero, the coldest possible temperature.");
}
else {
[fahrenheitConversion convert: (((((celsius)*9)/5)+32))];
NSLog(@"%lf degrees Celsius is %lf Fahrenheit", celsius, [fahrenheitConversion formula]);
}
}
else {
NSLog(@"Please enter a temperature in Fahrenheit to be converted into Celsius!:\n");
scanf("%lf", &fahrenheit);
if(fahrenheit < -459.67) {
NSLog(@"It is impossible to convert temperatures less than −459.67 degrees Fahrenheit, because this is absolute zero, the coldest possible temperature.");
}
else {
[celsiusConversion convert: ((((fahrenheit - 32)/9)*5))];
NSLog(@"%lf degrees Fahrenheit is %lf Celsius", fahrenheit, [celsiusConversion formula]);
}
}
return 0;
}
//将华氏温度转换为摄氏温度和摄氏温度转换为华氏温度的简单程序
#进口
@接口转换器:NSObject
{
//实例变量,用于存储对象的转换公式
双公式;
}
//声明用于设置和获取实例变量的实例方法
-(void)转换:(double)表达式;
-(双重)公式;
@结束
@实现转换器;
//定义实例方法以将参数(表达式)设置为等于实例变量
-(void)转换:(double)表达式
{
公式=表达式;
}
//定义返回实例变量的实例方法,公式
-(双)公式
{
回报公式;
}
@结束
int main(int argc,char*argv[])
{
//将两个新对象(一个用于华氏转换,另一个用于摄氏转换)指向Converter类
转换器*华氏转换=[[Converter alloc]init];
Converter*celsiusConversion=[[Converter alloc]init];
//声明两个保存用户输入数据的双变量,以及一个用于if-else语句的整数变量
双华氏度,摄氏度;
int提示;
NSLog(@“请按0将摄氏度转换为华氏度,或按1将华氏度转换为摄氏度\n”);
scanf(“%d”,提示(&P);
如果(提示==0){
NSLog(@“请输入一个以摄氏度为单位转换为华氏度的温度:\n”);
scanf(“%lf”&摄氏度);
如果(摄氏度<-273.15){
NSLog(@“不可能将温度转换为低于−273.15摄氏度,因为这是绝对零度,可能是最冷的温度。”);
}
否则{
[华氏转换转换率:((((摄氏)*9)/5)+32];
NSLog(@“%lf摄氏度为%lf华氏度”,摄氏度,[华氏转换公式];
}
}
否则{
NSLog(@“请输入要转换为摄氏度的华氏温度!:\n”);
扫描频率(“%lf”和华氏度);
如果(华氏<-459.67){
NSLog(@“不可能将温度转换为低于−459.67华氏度,因为这是绝对零度,可能是最冷的温度。”);
}
否则{
[Celsiuconversion转换:(((华氏-32)/9)*5));
NSLog(@“%lf华氏度为%lf摄氏度”)、华氏度[celsiusConversion公式];
}
}
返回0;
}
移植它应该非常简单。苹果提供了一些很好的示例应用程序。您最快的成功之路可能是下载一个简单的textfield+按钮示例并添加您的计算。不包括下载和安装所需的时间,您将在1-2小时内准备就绪
祝你好运 移植它应该非常简单。苹果提供了一些很好的示例应用程序。您最快的成功之路可能是下载一个简单的textfield+按钮示例并添加您的计算。不包括下载和安装所需的时间,您将在1-2小时内准备就绪 祝你好运 这个怎么样 我尽量不改变你的代码太多。它使用了您原来的公式和转换器类,尽管我试图更改它 截图:
- 下载(29kb)
#import <UIKit/UIKit.h>
@interface TempCalc_iPhoneViewController : UIViewController {
UITextField *celciusTextField;
UITextField *fahrenheitTextField;
UILabel *statusLabel;
double minFahrenheit;
double minCelcius;
NSString *normalStatus;
NSString *belowFahrenheitMessage;
NSString *belowCelciusMessage;
}
@property (nonatomic,retain) IBOutlet UITextField *celciusTextField;
@property (nonatomic,retain) IBOutlet UITextField *fahrenheitTextField;
@property (nonatomic,retain) IBOutlet UILabel *statusLabel;
- (IBAction) convertCelciusToFahrenheit:(UITextField*)sender;
- (IBAction) convertFahrenheitToCelcius:(UITextField*)sender;
@end
#导入
@接口TempCalc_iPhoneViewController:UIViewController{
UITextField*celciusTextField;
UITextField*华氏Extfield;
UILabel*状态标签;
双华氏度;
重瓣细鳞毛;
NSString*正常状态;
NSString*位于FahrenHeitMessage下方;
NSString*belowCelciusMessage;
}
@属性(非原子,保留)IBOutlet UITextField*celciusTextField;
@属性(非原子,保留)IBOutlet UITextField*华氏温度TextField;
@属性(非原子,保留)IBUILabel*statusLabel;
-(iAction)ConvertCelciUstofAhernheit:(UITextField*)发送方;
-(iAction)转换华氏温度:(UITextField*)发送方;
@结束
视图控制器实现
#import "TempCalc_iPhoneViewController.h"
#import "Converter.h"
@implementation TempCalc_iPhoneViewController
@synthesize celciusTextField, fahrenheitTextField, statusLabel;
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
[super viewDidLoad];
minCelcius = -273.15;
minFahrenheit = -459.67;
normalStatus = @"Please type to convert temperature";
belowFahrenheitMessage = @"impossible to convert temperatures less than −459.67 degrees Fahrenheit";
belowCelciusMessage = @"impossible to convert temperatures less than −273.15 degrees Celsius";
}
// Delegate method
- (BOOL)textFieldShouldReturn:(UITextField *)theTextField
{
NSLog(@"User pressed the DONE button on the keyboard (any keyboard!)");
[theTextField resignFirstResponder];
return YES;
}
- (IBAction) convertCelciusToFahrenheit:(UITextField*)sender
{
Converter *celciusConversion = [[Converter alloc] init];
double celcius = [sender.text doubleValue];
if(celcius < minCelcius) {
NSLog(@"It is impossible to convert temperatures less than −273.15 degrees Celsius, because this is absolute zero, the coldest possible temperature.");
self.statusLabel.text = belowCelciusMessage;
}
else
{
[celciusConversion convert: ((((celcius)*9)/5)+32)];
NSString *fahrenheitValue = [NSString stringWithFormat:@"%lf", [celciusConversion formula]];
NSLog(@"%lf degrees Celsius is %@ Fahrenheit", celcius, fahrenheitValue);
self.fahrenheitTextField.text = fahrenheitValue;
self.statusLabel.text = normalStatus;
}
[celciusConversion release];
}
- (IBAction) convertFahrenheitToCelcius:(UITextField*)sender
{
Converter *fahrenheitConversion = [[Converter alloc] init];
double fahrenheit = [sender.text doubleValue];
if(fahrenheit < minFahrenheit) {
NSLog(@"It is impossible to convert temperatures less than −459.67 degrees Fahrenheit, because this is absolute zero, the coldest possible temperature.");
self.statusLabel.text = belowFahrenheitMessage;
}
else {
[fahrenheitConversion convert: (((fahrenheit - 32)/9)*5)];
NSString *celciusValue = [NSString stringWithFormat:@"%lf", [fahrenheitConversion formula]];
NSLog(@"%lf degrees Fahrenheit is %@ Celsius", fahrenheit, celciusValue);
self.celciusTextField.text = celciusValue;
self.statusLabel.text = normalStatus; }
[fahrenheitConversion release];
}
#导入“TempCalc_iPhoneViewController.h”
#导入“Converter.h”
@实现TempCalc_iPhoneViewController
@合成celciusTextField、Fahrenheitextfield、statusLabel;
//实现viewDidLoad以在加载视图(通常从nib)后执行附加设置。
-(无效)viewDidLoad{
[超级视图下载];
minCelcius=-273.15;
华氏温度=-459.67;
normalStatus=@“请键入以转换温度”;
BellowFahrenheitMessage=@“无法转换低于以下温度的温度−459.67华氏度”;
belowCelciusMessage=@“无法转换低于以下的温度−273.15摄氏度”;
}
//委托方法
-(BOOL)文本字段应返回:(UITextField*)文本字段
{
NSLog(@“用户按下键盘上的“完成”按钮(任何键盘!)”;
[文本字段辞职第一响应者];
返回YES;
}
-(iAction)ConvertCelciUstofAhernheit:(UITextField*)发送方
{
Converter*celciusConversion=[[Converter alloc]init];
double celcius=[sender.text doubleValue];
if(塞尔修斯<明塞尔修斯){
NSLog(@“不可能将温度转换为低于−273.15摄氏度,因为这是绝对零度,可能是最冷的温度。”);
self.statusLabel.text=belowCelciusMessage;
}
其他的
{
[celcius版本转换:(((celcius)*9)/5)+32];
NSString*fahrenheitValue=[NSString stringWithFormat:@“%lf”,[celciusConversion formula]];
NSLog(@“%lf摄氏度为%@华氏度”,摄氏度,华氏值);
self.fahrenheitextfield.text=华氏温度