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

No comments:

Post a Comment