Implement UIButton, UITextField and UILabel programmatically in iPhone

This is the very simple application. In this application we will see how to implement TextField, Label and button programmatically in iPhone. So let see how it will work.

Step 1: Open the Xcode, Create a new project using View Base application. Give the application “Example”.

Step 2: Xcode automatically creates the directory structure and adds essential frameworks to it. You can explore the directory structure to check out the content of the directory.

Step 3: We need to add one resource in resource folder. Give the resource name “anim.png”.

Step 4: Open the ExampleViewController.h file and make the following changes in the file:

#import <UIKit/UIKit.h>
@interface ExampleViewController : UIViewController {

UITextField *textFieldRounded;
UILabel *label;
UIImageView *image;
UISlider *slider;

}

@property(nonatomic,retain) UITextField *textFieldRounded;
@property(nonatomic,retain) UILabel *label;
@property(nonatomic,retain) UIImageView *image;
@property(nonatomic,retain) UISlider *slider;

@end

Step 5: In the Example.m file make the following changes in the file:

#import "ExampleViewController.h"

@implementation ExampleViewController

@synthesize textFieldRounded,label,image,slider;

(void)dealloc
{
    [super dealloc];
    [textFieldRounded release];
    [label release];
    [image release];
    [slider release];
}

(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.
}

#pragma mark – View lifecycle

// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
(void)viewDidLoad
{
    [super viewDidLoad];
   
   textFieldRounded = [[UITextField alloc] initWithFrame:CGRectMake(20, 50, 280, 31)];
    textFieldRounded.borderStyle = UITextBorderStyleRoundedRect;
    textFieldRounded.textColor = [UIColor blackColor];
    textFieldRounded.font = [UIFont systemFontOfSize:17.0];
    textFieldRounded.placeholder = @"Enter Your name";  //place holder
    textFieldRounded.backgroundColor = [UIColor whiteColor];
    textFieldRounded.autocorrectionType = UITextAutocorrectionTypeNo;  
     textFieldRounded.backgroundColor = [UIColor clearColor];
    textFieldRounded.keyboardType = UIKeyboardTypeDefault;  
    textFieldRounded.returnKeyType = UIReturnKeyDone;  
   
    textFieldRounded.clearButtonMode = UITextFieldViewModeWhileEditing;
   
    [self.view addSubview:textFieldRounded];
   
    textFieldRounded.delegate = self;
   
     
    UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [button addTarget:self
               action:@selector(buttonPressed:)
     forControlEvents:UIControlEventTouchDown];
    [button setTitle:@"Button" forState:UIControlStateNormal];
    button.frame = CGRectMake(20, 108, 97, 37);
    [self.view addSubview:button];
   
   
    label = [[UILabel alloc] initWithFrame:CGRectMake(40, 243, 240, 21)];
    label.backgroundColor = [UIColor clearColor];
    label.textAlignment = UITextAlignmentCenter; // UITextAlignmentCenter, UITextAlignmentLeft

    label.text = @"Label";
    [self.view addSubview:label];
   
   
   
    CGRect myImageRect = CGRectMake(40.0f, 190.0f, 240.0f, 128.0f);
    image = [[UIImageView alloc] initWithFrame:myImageRect];
    [image setImage:[UIImage imageNamed:@"anim.png"]];
     image.backgroundColor = [UIColor clearColor];
    image.opaque = YES;
    image.alpha = 0;
    [self.view addSubview:image];
    [image release];
   
    CGRect frame = CGRectMake(18.0, 348.0, 284.0, 23.0);
    slider = [[UISlider alloc] initWithFrame:frame];
    [slider addTarget:self action:@selector(sliderAction:) forControlEvents:UIControlEventValueChanged];
    [slider setBackgroundColor:[UIColor clearColor]];
    slider.continuous = YES;
    slider.value = 0.0;
    [self.view addSubview:slider];
   
}

(IBAction) sliderAction:(id) sender{
    image.alpha = slider.value;
}

(IBAction) buttonPressed:(id) sender{
    [textFieldRounded resignFirstResponder];
        label.text = textFieldRounded.text;
}

(BOOL)textFieldShouldReturn:(UITextField *)textField{
        [textField resignFirstResponder];
        return YES;
}

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

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

@end

Step 6: Now compile and run the application on the simulator.

You can Download SourceCode from here Example

Leave a Reply

Your email address will not be published. Required fields are marked *