iphone - 将极其基本的 objective-c 程序连接到 iPhone 应用程序

标签 iphone c objective-c ios4

我想知道将我在 objective-c 中的这个简单的控制台程序连接到一个非常简单的 iPhone 应用程序中是否可行。

由于我没有使用 Interface Builder 的经验,我不确定我需要多长时间才能学会它。

此外,我认为我的代码必须针对某些 iPhone API 进行调整,而不是使用控制台进行输入和输出。

这是程序的 main.m 文件,其中存储了所有代码:

//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;
}

最佳答案

这个怎么样?

我尽量不对您的代码进行过多改动。它使用您的原始公式和转换器类,尽管我很想更改它。

截图:

alt text http://brockwoolf.com/shares/stackoverflow/3443063/tempcalc-iphone.png

源代码:

ViewController 接口(interface):

#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

ViewController 实现

#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];
}

关于iphone - 将极其基本的 objective-c 程序连接到 iPhone 应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3443063/

相关文章:

ios - 防止 friend 数组中的重复项

iphone - 使用SocializeActionBar添加多个实体

iphone - 如何更新 NSMutableDictionary

c - 期望的常量表达式

objective-c - iPad - 同一屏幕中有多个 UITableView

objective-c - 使用 KVO 时是否有必要在 -dealloc 中将 self 作为 self 的观察者移除?

iphone - AVAsset 和 AVAssetTrack - IOS 4.0 中的跟踪管理

iphone - 当当前选定的表格单元格移出屏幕后,如何向其发送消息?

ios - 为 iPad 2 进行开发

C中int数组和char数组的串联