Search Posts in my Blog

Showing posts with label iphone programming. Show all posts
Showing posts with label iphone programming. Show all posts

Monday, 25 June 2012

Using the New Stepper Control in iPhone




In Xcode 4 and iOS version 5, we have a new control available for use: the UIStepper control. The stepper control allows the user to increment or decrement a value. In this blog, we will see how to use stepper controls to implement a simple four – digit combination lock. So, let’s get started!
First, open Xcode and select “Create a new Xcode project.” Choose the Single View Application template, and click Next. Name the project “StepperLock” and choose options as shown below:
Click Next, choose a location to save the file, and click Create.
Once the application has been created, open the MainStoryboard.storyboard file by selecting it in the Project Navigator. Drag four UIStepper controls from the library to the view, and also drag a single UIButton and two UILabels. Change the text on the button to “Enter” and the text on the two labels to “Enter your combination” and “0000” . Your user interface should look something like:
Select all four of the stepper controls by dragging a selection box around them. In the Attributes inspector panel, change the Maximum value to 9 as shown:
This will change the value for all four of the stepper controls. If you wish, you may change the background color of the view to something more attractive.
Open the ViewController.h file, and add the properties and methods shown in bold below.
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController
@property (nonatomic, strong)IBOutlet UILabel *combination;
@property (nonatomic, strong)NSString *correctCombination;
@property (nonatomic, strong)IBOutlet UIStepper *stepper1;
@property (nonatomic, strong)IBOutlet UIStepper *stepper2;
@property (nonatomic, strong)IBOutlet UIStepper *stepper3;
@property (nonatomic, strong)IBOutlet UIStepper *stepper4;
- (IBAction)stepperValueChanged:(UIStepper *)sender;
- (IBAction)checkCombination:(UIButton *)sender;
@end
Now, return to the MainStoryboard.storyboard file, and wire up all of the controls as shown below. Note that all four steppers are wired to the “stepperValueChanged” method. We will user the value of each stepper to build the string in the combination label. When the Enter button is touched, the value in the label will be compared to the value of the correctCombination string. If the values are equal, we’ll set the background color of the main view to green, else we’ll set it to a red color.
Open the ViewController.m file, and add the bolded code shown here:
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
@synthesize combination;
@synthesize correctCombination;
@synthesize stepper1, stepper2, stepper3, stepper4;
- (IBAction)stepperValueChanged:(UIStepper *)sender
{
    self.combination.text = [NSString stringWithFormat:@"%d%d%d%d",
                             (long)stepper1.value, (long)stepper2.value,
                             (long)stepper3.value, (long)stepper4.value];
}
// listing continues:
- (IBAction)checkCombination:(UIButton *)sender
{
    if ([self.combination.text isEqualToString:self.correctCombination]) {
        self.view.backgroundColor = [UIColor greenColor];
    } else {
        self.view.backgroundColor = [UIColor redColor];
    }
}
- (void)viewDidLoad
{
    [super viewDidLoad];
        // Do any additional setup after loading the view, typically from a nib.
    self.correctCombination = @"1961";
}
…
As usual, we first synthesize all of the properties declared in the header file. Then, the two action methods are defined.
In stepperValueChanged, we use the class method stringWithFormat (of NSString) to build a string comprised of the values of the four stepper controls. The value of a stepper control is a C double primitive type; note we cast each value to a long (long int) in order to display it as an integer type with the format specifier “%d” .
The next method (checkCombination) simply compares the text in the combination label to the text in the correctCombination string. If they are the same, the view’s background is set to green, otherwise, it is set to red. In a real application, we might push another view controller instead.
Run the application to verify that it works properly.

Monday, 27 February 2012

Introduction to mobile technology

iPhone are touch screen mobile phones which run on IOS which is a Apple's mobile Operating System and is derived from MAC OSX. Earlier when iPhone was launched in some what around Jan 2007 third party applications were not supported in iPhone but soon Apple announced that they will be introducing certain SDK (Software development kit) with the help of which developers from all around the world can build their own application.


When you are working on any mobile app their are some key features which you must always keep in mind :

  1. Memory: Mobile device have limited memory and a lot of it is already allocated.
  2. Display Capabilities: Different mobile devices have different display size and you need to plan for that.
  3. App speed: Mobile app users want their app to work quickly so as a mobile app developer you must see to it that you app load and work quickly.
  4. User Interface: The user interface of your app must be very friendly.
Now it's time to get some tools. Go to http://developer.apple.com/iphone/index.action
You can download the SDK for free with the basic registration, but to distribute a completed app on the App Store or install your app on the iPhone for testing you need to become a paid
Standard or Enterprise Developer. The SDK comes with a simulator for testing
directly on your Mac, so free registration is all you’ll need for now.

Now let's talk about the IDE where all the coding part will take place but before that we will have a look at the programming language that we will be using to make an iPhone app. The language used is Objective C which is an object oriented programming language it derives it's syntax from Small Talk and C programming. The IDE used for iPhone development and desktop app development for Apple is Xcode, Xcode 4.2 is the latest version. The Xcode suite also includes most of Apple's developer documentation, Interface Builder, modified version of GNU Compiler and supports C, C++, FORTRAN, Objective C, Objective C++, Java, Apple script, Ruby, Python the Xcode uses GNU debugger as a back end for it's debugger. The Cocoa and Cocoa Touch frameworks powers Mac OS X and I Phone OS and are tightly integrated into the Xcode development experience. Cocoa’s high-level APIs make is easy to add animation, networking, and the native platform appearance and behavior to your application with only a few lines of code.

This was a small introduction to XCode and IOS. I will be adding some examples on some of the controls in Xcode later. I hope that this blog was helpful to you. In order to know the features of iphone you can just hit this link iPhone Features