Category Archives: Iphone

iOS – UIStepper Example…

UIStepper was introduced in iOS 5.0.It provides an option for the user to increment or decrement values.Now we are going to create a project with UIStepper which displays increment or decrement values.

  • open Xcode and Select Create a new Xcode project –>Single View Application.
  • Enter the product name as ‘UIStepperExample‘,this will remain as your project name.
  • Enter company identifier,organizaton name,bundle identifier.
  • Check option Use Automatic Reference Counting and uncheck option Use Storyboards.

 

  • Drag and drop the UIStepper and UILabel in the .xib file like,

 

  • ctrl + drag from UIStepper to interface section to create an outlet for UIStepper and name it as ‘stepperOutlet‘.

 

  • ctrl+drag from UILabel to interface section to create an outlet for UILabel and name it as ‘stepperLabel‘.

 

  • ctrl+drag from UIStepper to interface section to create an Action for UIStepper and name it as ‘stepperClicked‘.

 

  • Now add this code to ViewController.m file inside the ‘viewDidLoad‘ method.

self.stepperOutlet.minimumValue = 0;

self.stepperOutlet.maximumValue = 1000;

self.stepperOutlet.stepValue = 10;

self.stepperOutlet.autorepeat = YES;

self.stepperOutlet.continuous = YES;

self.stepperLabel.text = [NSString stringWithFormat:@"Value:%d",0];

What’s this,

Minimum Value:It is the minimum value the UIStepper will start from.Here we our minimum value is ‘0‘.

Maximum Value:It is the maximum value of the UIStepper can reach.Here our maximum value is ‘1000‘.

Step Value:It is the value by which UIStepper will increment or decrement each time when we press ‘+‘ or ‘‘ button.

Autorepeat:If ‘YES’ the UIStepper will continuously increment or decrement the value when the ‘+‘ or ‘‘ button is press and hold.

Continuous:If ‘YES’ the value change events are send immediately when the user clicks ‘+‘ or ‘‘ button.If ‘NO’ the value changing events are send when the user interaction ends.

 

  • Now add this code inside the method ‘stepperClicked‘,

int stepperIntValue = self.stepperOutlet.value;
self.stepperLabel.text = [NSString stringWithFormat:@"Value:%d",stepperIntValue];

Congratulation!

Advertisements

iOS – UIPageControl example…

A Pagecontrol presents user with a set of horizontal dots representing pages.The current page is presented as white dot.We can navigate to the next or previous pages.In this example we will create a page control with displaying page number. 

  • Create a new project as, Create a new Xcode project –>Single View Application
  • Enter the Product name as PageControlExample.This will be your project name.
  • Enter Organization name,company identifier,bundle identifier and check use Automatic Reference counting.Uncheck the use Storyboard option.
  • Drag and drop the UIPageControl in the .xib file.It looks like,

  • ctrl+drag from pagecontrol to interface section to create an outlet.

pagecontrol

  • Drag and drop a label in the .xib file and create an outlet like we did before.
  •   create an action for the pagecontrol and name it as changePage,

Now add this lines inside the changePage method ,

self.pageNumber.text = [NSString stringWithFormat:@"%i", ([self.pageControl currentPage]+1)];

Congratulation !  Finally…

iOS – UIProgressView Example…

ProgressView is used to show progress of work.For example downloading,retrieving values from web service.

Create the UIProgressView by setting referencing outlets with getter and setter property.

@property (strong, nonatomic) IBOutlet UIProgressView *progressObject;

@synthesize progressObject;

Create a button with method named,

- (IBAction)startProgress:(id)sender;

Implement the methods in the .m file,

static float progress = 0.0f;

-(void)showWithProgress

{

progress = 0.0f;

progressObject.progress  = progress;

if(progressObject.progress<1)

[self performSelector:@selector(increaseProgress) withObject:nil afterDelay:0.3];

}

-(void)increaseProgress {

progress+=0.1f;

progressObject.progress = progress;

if(progress < 1.0f)

[self performSelector:@selector(increaseProgress) withObject:nil afterDelay:0.3];

}

Now just call these methods like this,

- (IBAction)startProgress:(id)sender

{

[self showWithProgress];

}

OUTPUT:

progressview

PROPERTIES:

progressImage

An image to use for the portion of the progress bar that is filled.

@property(nonatomic, retain) UIImage *progressImage
  • Available in iOS 5.0 and later.

ProgressTintColor

The color shown for the portion of the progress bar that is filled.

@property(nonatomic, retain) UIColor *progressTintColor
  • Available in iOS 5.0 and later.

ProgressViewStyle

The current graphical style of the receiver.

@property(nonatomic) UIProgressViewStyle progressViewStyle
  • Available in iOS 2.0 and later.

TrackImage

An image to use for the portion of the track that is not filled.

@property(nonatomic, retain) UIImage *trackImage

TrackTintColor

The color shown for the portion of the progress bar that is not filled.

@property(nonatomic, retain) UIColor *trackTintColor
  • Available in iOS 5.0 and later.

iOS – UIActivityIndicatorView example…

 UIActivityIndicatorView is used to show that some long process is processing in background.It can be implemented programatically and using IBOutlet.

In this tutorial we are going to create a UIActivityIndicatorView programatically with start and stop animations.Lets get started,

Step 1: Create a new project in Xcode.

Step 2: Set property for UIActivityIndicatorView in .h file and set @synthesis in .m file.

.h file: 

@property (nonatomic,retain) UIActivityIndicatorView *activityIndicatorObject;

.m file:

@synthesis activityIndicatorObject;

Step 3: Now Initialize UIActivityIndicatorView object and set position.

// Initialize ActivityIndicator object

    activityIndicatorObject = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];

// Set Center Position for ActivityIndicator

    activityIndicatorObject.center = CGPointMake(150, 150);

   // Add ActivityIndicator to your view

    [self.view addSubview:activityIndicatorObject];

Step 4: Add two buttons in xib and set Action as stopAnimating and startAnimating.These methods are used to start and stop ActivityIndicator.

- (IBAction)startAnimating:(id)sender

{

[activityIndicatorObject startAnimating]; 

}

 - (IBAction)stopAnimating:(id)sender

{

[activityIndicatorObject stopAnimating];

}

Step 5: Now Run the project.You are Done!!!

 

iOS – UISwitch example…

UISwitch is an component which resembles the electronic switch with animation.We can change the Switch state by manually or programatically.

In this tutorial we are going to change the state of the UISwitch programatically with suitable changing Label text.Lets have a look at this,

Step 1: Create a new ‘Single view’ project in Xcode.

Step 2: Drag and drop components like UISwitch,UILabel and UIButton.Arrange them as shown below,

Step 3:  Right click over the ‘UISwitch’ component.Now drag and drop in the .h file as shown below.Give a name to your UISwitch and UILabel component which will be referenced later.Set method name for the UISwitch and Button as we did before.

@property (weak, nonatomic) IBOutlet UISwitch *switchObject;

@property (weak, nonatomic) IBOutlet UILabel *switchLabel;

- (IBAction)toggleAction:(id)sender;

- (IBAction)switchAction:(id)sender;

Step 4: Include this method in your .m file.

- (IBAction)toggleAction:(id)sender {

    if(switchObject.on)

    {

        [switchObject setOn:NO animated:YES];

                       switchLabel.text  = @"Disabled";

    }

    else

    {

 [switchObject setOn:YES animated:YES];

        switchLabel.text  = @"Enabled";

            }

}

In this method the UISwitch state and label value changes when user pressed the ‘Toggle’ button.

Step 5:Include this method in your .m file.

- (IBAction)switchAction:(id)sender {

    if(switchObject.on)

    {

       switchLabel.text  = @"Enabled";

    }

    else

    {

        switchLabel.text  = @"Disabled";

        }

}

In this method the label value changes when user pressed the UISwitch.

Step 6: Now Run the project.You are done…

iOS – UISlider Example…

UISlider is an visual object used to select a single value from a continuous range of values.Values can be selected by sliding.

Step 1: Drag and drop a UITextField and a UISlider in the xib.

Step 2: Set the reference for UISlider and UITextField in the .h file.UISlider as ‘slider’ and UITextField as ‘sliderTextField’.

@property (weak, nonatomic) IBOutlet UISlider *slider;

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

Step 3: Set an action method for UISlider as ‘sliderProgress’.

- (IBAction)sliderProgress:(id)sender;

Step 4: Enter this code inside the method ‘sliderProgress’.

 sliderTextField.text = [NSString stringWithFormat:@"%f",slider.value];

Step 5: We are done now,Run the Program !

iPhone – UITextField Delegate Methods…

So its a long time….Okkk.Let me tell you about the delegates and those methods.Here we go,

What is Delegate:

A delegate is an object which communicates with other objects when an event occurs.If you keep your fingers over a candle light you get a message to your brain which says to take back,its a kinda delegate.

UITextField Delegate Methods:

Every component has some delegate methods which will be called when an  event occurs.TextField delegate methods are,

*textFieldShouldBeginEditing

*textFieldShouldEndEditing

*textFieldDidBeginEditing

*textFieldDidEndEditing

*textField shouldChangeCharactersInRange

*textFieldShouldClear

*textFieldShouldReturn

Now Lets have a Deep look over them,

textFieldShouldBeginEditing:

This method asks  some textfield to become the first responder(Current TextField).So you can change the delegate information.

textFieldDidEndEditing:

This method asks  some textfield to resign the first responder(Current TextField).So you can change the delegate information.

textFieldDidBeginEditing:

This methods tells that some textfield has just became the first responder(Current TextField).So you can change the delegate information.

textFieldDidEndEditing:

This methods tells that some textfield has just resigned the first responder(Current TextField).So you can change the delegate information.

textField shouldChangeCharactersInRange:

This method is called whenever user types a new character or delete’s a character.

textFieldShouldClear:

This method is used to clear the textfield which is an build in method.

textFieldShouldReturn:

This method is called when the user enter’s the return button.So we can add some additional behaviour here.

 Example:

Step 1: As i have explained already i have created an Xcode project.

Step 2: The xib file looks like this.I have two textfields for UserName and Password with a button.

screen

Step 3: I have created outlet and set delegate to the textfields.

Setdelegate

Step 4: Implement UITextFieldDelegate in the .h file.

setDelegateinInterfaceStep 5: I have included the QuartzCore framework and imported the file in my .m file.Using this you can make some overlay colouring,modify border width and more.

import

Step 6: Implement your logic inside the delegate methods.

Now you are set to Go….

Code:

.h File:

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController<UITextFieldDelegate>

@property (retain, nonatomic) IBOutlet UITextField *userNameTextfield;

@property (retain, nonatomic) IBOutlet UITextField *passwordTextfield;

– (IBAction)submitClicked:(id)sender;

@end

 .m File:

#import “ViewController.h”

#import <QuartzCore/QuartzCore.h>

@interface ViewController ()

@end

@implementation ViewController

@synthesize userNameTextfield,passwordTextfield;

– (void)viewDidLoad

{

[super viewDidLoad];

}

– (void)didReceiveMemoryWarning

{

[super didReceiveMemoryWarning];

}

– (void)dealloc {

[userNameTextfield release];

[passwordTextfield release];

[super dealloc];

}

– (IBAction)submitClicked:(id)sender {

if(userNameTextfield.text.length == 0 || passwordTextfield.text.length==0)

{

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@”Please Enter UserName and Password” message:nil delegate:self cancelButtonTitle:@”Ok” otherButtonTitles:nil];

[alert show];

}

else

{

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@”You are learning about delegates….” message:nil delegate:self cancelButtonTitle:@”Ok” otherButtonTitles:nil];

[alert show];

}

}

#pragma  – mark Textfield Delegate Methods

– (BOOL)textFieldShouldBeginEditing:(UITextField *)textField

{

textField.layer.borderWidth=2;

textField.layer.borderColor=[[UIColor redColor] CGColor];

textField.layer.cornerRadius=7;

return YES;

}

– (void)textFieldDidBeginEditing:(UITextField *)textField  {

}

– (BOOL)textFieldShouldEndEditing:(UITextField *)textField

{

textField.layer.borderColor = [[UIColor  clearColor] CGColor];

return YES;

}

– (void)textFieldDidEndEditing:(UITextField *)textField {

}

– (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string  {

return YES;

}

– (BOOL)textFieldShouldClear:(UITextField *)textField  {

return NO;

}

– (BOOL)textFieldShouldReturn:(UITextField *)textField

{

if(textField == userNameTextfield)

{

[passwordTextfield becomeFirstResponder];

}

else if (textField == passwordTextfield)

{

[passwordTextfield resignFirstResponder];

}

return YES;

}

@end