Showing posts with label Xcode. Show all posts
Showing posts with label Xcode. Show all posts

Tuesday, February 21, 2012

XCODE - Source Code + GUI "Calculator v1.0"

View Controller :
calViewController.h

#import <UIKit/UIKit.h>

@interface calViewController : UIViewController
{   /*  Declare variable as private   */
    double currentNumber;   // store current value
    double storeNumber;     // store old value
    char Operator;          // store sign of operator
    bool point;             // condition to floating point
    int pointCount;         // condition to number of floating point
}
// Prototype Function to use
// getCurrentNumber
- (void) getCurrentNumber:(double)number;
- (void) showCurrentNumber;
- (void) showStoreNumber;
/*  Label for Showing number    */
@property (weak, nonatomic) IBOutlet UIButton *bEqualOutlet;
@property (weak, nonatomic) IBOutlet UILabel *labelShow;
/*  Button for all operand and operator */
- (IBAction)b:(id)sender;
@property (weak, nonatomic) IBOutlet UIButton *ac;

- (IBAction)bPlus:(id)sender;
- (IBAction)bMinus:(id)sender;
- (IBAction)bMulti:(id)sender;
- (IBAction)bDivid:(id)sender;
- (IBAction)bClear:(id)sender;
- (IBAction)bPower:(id)sender;
- (IBAction)bEqual:(id)sender;
- (IBAction)bPoint:(id)sender;

@end


calViewController.m

#import "calViewController.h"

@implementation calViewController
@synthesize ac;
@synthesize bEqualOutlet;
@synthesize labelShow;
/*  Start Function    */
//Function getCurrentNumber
- (void) getCurrentNumber:(double)number{
    if(point == false){
        currentNumber = currentNumber*10 + number;
    }else{
        currentNumber += number/pow(10,++pointCount);
    }
}
//Function getLastStore
- (void) getLastStore{
    if(Operator == '+'){
        storeNumber += currentNumber;
    }else if(Operator == '-'){
        storeNumber -= currentNumber;
    }else if(Operator == '*'){
        storeNumber *= currentNumber;
    }else if(Operator == '/'){
        storeNumber /= currentNumber;
    }else if(Operator == '^'){
        storeNumber = pow(storeNumber,currentNumber);
    }
    currentNumber = 0;
}
//Function showCurrentNumber
- (void) showCurrentNumber{
    self.labelShow.text = [NSString stringWithFormat:@"%g",currentNumber];
}

//Function showStoreNumber
-(void) showStoreNumber{
    self.labelShow.text = [NSString stringWithFormat:@"%g",storeNumber];
}
/*  End Function   */
- (IBAction)b:(id)sender{  // button 0
    double n = [sender tag];
    [self getCurrentNumber:n];
    [self showCurrentNumber];
}
- (IBAction)bPlus:(id)sender{          // button Plus +
    [self getLastStore];
    [self showStoreNumber];
    Operator = '+';
    point = false;
    pointCount = 0;
}
- (IBAction)bMinus:(id)sender{          // button Minus -
    [self getLastStore];
    [self showStoreNumber];
    Operator = '-';
    point = false;
    pointCount = 0;

}
- (IBAction)bMulti:(id)sender{          // button Multi *
    [self getLastStore];
    [self showStoreNumber];
    Operator = '*';
    point = false;
    pointCount = 0;

}
- (IBAction)bDivid:(id)sender{          // button Divid /
    [self getLastStore];
    [self showStoreNumber];
    Operator = '/';
    point = false;
    pointCount = 0;

}
- (IBAction)bClear:(id)sender{          // button Clear AC
    currentNumber = 0;
    storeNumber = 0;
    point = false;
    pointCount = 0;
    Operator = '+';
    [self showStoreNumber];
}
- (IBAction)bPower:(id)sender{          // button Power ^
    [self getLastStore];
    [self showStoreNumber];
    Operator = '^';
    point = false;
    pointCount = 0;

}
- (IBAction)bEqual:(id)sender{          // button Equal =
    [self getLastStore];
    [self showStoreNumber];
    point = true;
    pointCount = 0;
    Operator = '+';
}
- (IBAction)bPoint:(id)sender{          // button Point .
    point = true;
    pointCount = 0;
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    /*  Constructure */
    currentNumber = 0;
    storeNumber = 0;
    Operator = '+';
    point = false;
    pointCount = 0;
    // Do any additional setup after loading the view, typically from a nib.
}

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

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
}

- (void)viewDidDisappear:(BOOL)animated
{
    [super viewDidDisappear:animated];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}

@end


Tuesday, February 7, 2012

XCODE - Source Code + GUI " Function2 " [example]

1.Lanuch Xcode program (recommand 4.2 / iOS 5)  then add new project , choose Single View Application. click on button Next
click on image to see original size
2.Type Product Name : Function2 as image below , Class Prefix : is set for prefix name of your project (not important to be change) ,  Device Family : iPhone / iPad / Universal, it depend on what device will you develop for. then click Next 
click on image to see original size
3.Choose directory/folder to save your project, click Create
click on image to see original size

click on image to see original size
4.Select MainStoryboard.storyboard to create user interface your device.
 
Select on this box from image below to show list of items to put on View Controller
click on image to see original size
select a Navigation Bar [UINavigationBar]

Drag Navigation Bar to View Controller on the Top of screen

Set a title name as Function2 as image below


Select Bar Button Item [UIBarButtonItem]

Drag Bar Button Item on the Navigation Bar

Select a Label [UILabel]

Drag Label drop on the View Controller then Change name by Double-click

Show The Attribute Inspector from image below to change font style of Label


Color of Label was changed to pink

Select Text Field and drag on View Controller for Input Item [UITextField]

Select Round Rect Button [UIButton]

Select Toolbar [UIToolbar]

Finally View Controller


5.Making View Controller's items be able to Connect with Code :


Select Editor in the middle from image below to show code with View Controller

click on image to see original size

6. Code :


XViewController.h
//
//  XViewController.h
//  Function2
//
//  Created by keovessna vong on 6/2/2012.
//  Copyright (c) 2012 Burapha. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface XViewController : UIViewController <UITextFieldDelegate>

@property (weak, nonatomic) IBOutlet UITextField *textA;

@property (weak, nonatomic) IBOutlet UITextField *textB;
@property (weak, nonatomic) IBOutlet UITextField *textC;
@property (weak, nonatomic) IBOutlet UILabel *labelX1;
@property (weak, nonatomic) IBOutlet UILabel *labelX2;

-(IBAction)cal:(id)sender;
-(IBAction)clear:(id)sender;

@end
XViewController.m
//
//  XViewController.m
//  Function2
//
//  Created by keovessna vong on 6/2/2012.
//  Copyright (c) 2012 Burapha. All rights reserved.
//

#import "XViewController.h"

@implementation XViewController
@synthesize textA;
@synthesize textB;
@synthesize textC;
@synthesize labelX1;
@synthesize labelX2;

-(IBAction)cal:(id)sender
{
    NSString *strA = [[NSString alloc]initWithString:[self.textA text]] ;
    NSString *strB = [[NSString alloc]initWithString:[self.textB text]] ;
    NSString *strC = [[NSString alloc]initWithString:[self.textC text]] ;
   
    int a,b,c ;
    a = [strA integerValue] ;
    b = [strB integerValue] ;
    c = [strC integerValue] ;
    float x1,x2;
    float Data = b*b - 4.0*a*c;
    if(Data<0){
        self.labelX1.text = @"NULL";
        self.labelX2.text = @"NULL";
       
    }else{
        x1 = (-b-pow(Data,0.5))/2/a;
        x2 = (-b+pow(Data,0.5))/2/a;
        self.labelX1.text = [NSString stringWithFormat:@"%f", x1] ;
        self.labelX2.text = [NSString stringWithFormat:@"%f", x2] ;
    }
   
}
-(IBAction)clear:(id)sender
{
    self.labelX1.text= @"";
    self.labelX2.text= @"";
    self.textA.text=@"";
    self.textB.text=@"";
    self.textC.text=@"";
   
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField{
    [textField resignFirstResponder];  // hide keyboard with return
    return true ;
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    NSLog(@"view did load");
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
}

- (void)viewDidDisappear:(BOOL)animated
{
    [super viewDidDisappear:animated];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
        return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
    } else {
        return YES;
    }
}
@end