ページ

2011年5月27日金曜日

XcodeでiPhone用Calculatorを作ってみた





calcViewController.h
#import <UIKit/UIKit.h>

@interface calcViewController : UIViewController {

 IBOutlet UILabel *answerDisplay;
 float answer;
 float currentNumber;
 int currentOperation;
}

-(IBAction) buttonDigitPressed:(id)sender;
-(IBAction) buttonOperationPressed:(id)sender;
-(IBAction) clear;
-(IBAction) allClear;

@end


calcViewController.m
#import "calcViewController.h"

@implementation calcViewController

-(IBAction) buttonDigitPressed:(id)sender{
 currentNumber = currentNumber * 10 + (float)[sender tag];
 answerDisplay.text = [NSString stringWithFormat:@"%10.3f", currentNumber];
}

-(IBAction) buttonOperationPressed:(id)sender{
 
 if(currentOperation == 0){
  answer = currentNumber;
 }else {
   switch (currentOperation) {
    case 1:
     answer = answer + currentNumber;
     break;
    case 2:
     answer = answer - currentNumber;
     break;
    case 3:
     answer = answer * currentNumber;
     break;
    case 4:
     answer = answer / currentNumber;
     break;
    default:
     currentOperation = 0;
     break;
   }
 }
 
 currentNumber = 0;
 answerDisplay.text = [NSString stringWithFormat:@"%10.3f", answer];
 currentOperation = [sender tag];
}

-(IBAction) clear{
 currentNumber = 0;
 answerDisplay.text = @"0";
}

-(IBAction) allClear{
 currentNumber = 0;
 answerDisplay.text = @"0";
 currentOperation = 0;
}

- (void)didReceiveMemoryWarning {
 // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
 
 // Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
 // Release any retained subviews of the main view.
 // e.g. self.myOutlet = nil;
}


- (void)dealloc {
    [super dealloc];
}

@end

参考


YouTube - XCode 4 Tutorial Basic Calculator - Geeky Lemon Development
http://www.youtube.com/watch?v=Ihw0cfNOrr4&hd=1


0 件のコメント:

コメントを投稿