2010-08-09 57 views
1

我想知道將我在objective-c中的這個簡單控制檯程序連接到非常簡單的iPhone應用程序是否可行。將極其基本的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; 
} 
+1

你可以,但你必須使用IB創建佈局和定義IBActions這將基本上是你在這裏定義的方法。 – 2010-08-09 18:44:43

+0

只是想知道,我是否會在類型爲Utility的iPhone下創建一個新的應用程序,在我看來它就像一個窗口小部件應用程序,它具有主屏幕,然後您可以將其翻轉到另一側以查看一些內容信息。 – Qcom 2010-08-09 18:51:14

回答

4

這個怎麼樣?

我試圖不要更改您的代碼太多。它使用你原來的公式和轉換器類,儘管我很想改變它。

截圖:

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

源代碼:

視圖控制器接口:

#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 

視圖控制器實現

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

哇!非常感謝你!所以它看起來像你唯一需要弄亂的文件是ViewController接口和實現文件?實際上有相當多的代碼在那裏你不得不寫:)再次感謝你,我真的從中學到很多東西! – Qcom 2010-08-09 20:31:52

+1

當然沒問題。開心iPhone編碼:) – 2010-08-09 20:32:51

+1

是的。我添加代碼的唯一文件是ViewController.h和.m文件。 IBOutlets和IBActions在Interface Builder中連接 – 2010-08-09 20:44:40

1

移植應該是很簡單的。 Apple提供了一些很好的示例應用你最快的成功之路可能是下載一個簡單的文本框+按鈕樣本並添加你的計算。不包括下載和安裝所需的時間將在1-2個小時內完成。

祝你好運!

+0

嘿謝謝!希望我能在這段時間內做到這一點:) – Qcom 2010-08-09 19:24:37

相關問題