【问题标题】:Connecting extremely basic objective-c program to iPhone application将非常基本的objective-c程序连接到iPhone应用程序
【发布时间】:2010-08-09 18:37:35
【问题描述】:

我想知道将我在 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;
}

【问题讨论】:

  • 您可以,但您必须使用 IB 来创建布局并定义您的 IBActions,这基本上就是您在此处定义的方法。
  • 只是想知道,我会不会在 iPhone 下创建一个新的应用程序,类型为 Utility,在我看来有点像小部件应用程序,它有主屏幕,然后你可以把它翻过来到另一边查看一些信息。

标签: iphone c objective-c ios4


【解决方案1】:

这个怎么样?

我尽量不要过多地更改您的代码。它使用你原来的公式和转换器类,尽管我很想改变它。

截图:

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

源代码:

ViewController 接口:

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

【讨论】:

  • 哇!非常感谢你!!所以看起来你唯一需要处理的文件是 ViewController 接口和实现文件?里面实际上有很多你必须写的代码:) 再次感谢你,我真的从中学到了很多!
  • 当然没问题。快乐的 iPhone 编码 :)
  • 是的。我添加代码的唯一文件是 ViewController.h 和 .m 文件。 IBOutlets 和 IBActions 在 Interface Builder 中连接
  • 只是想知道,这里有点离题,但是您会对程序进行哪些更改以优化它或完全改变其结构?正如你所知道的那样,我的内容非常基础,因为我正在尝试应用我从正在阅读的这本书中学到的东西。任何建议我都完全开放,我很感激!
  • @BOSS - 我会说 Brock 在架构方面做得很好,将这一点带到了 iPhone 上。他遵循模型-视图-控制器设计模式,将此逻辑放在控制器类中并连接适当的视图。你正在做的计算相当简单,所以我看不出有很多需要优化的地方。您可以将 Converter 类重塑为 NSValueTransformer,但这不是必需的。
【解决方案2】:

移植它应该非常简单。 Apple 提供了一些很好的示例应用程序。您最快的成功之路可能是下载一个简单的文本字段 + 按钮示例并添加您的计算。不包括下载和安装所需的时间,您将在 1-2 小时内准备好它。

祝你好运!

【讨论】:

  • 嘿,谢谢!希望我能在这段时间内做到这一点:)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-02-15
  • 1970-01-01
  • 2021-09-09
  • 1970-01-01
相关资源
最近更新 更多