Tuesday, 29 March 2011

UIAlert View

Display an AlertView on Button Touch

In this post i will be explaining how to display your name in an alert view.


Alert view: An alert view is like a message box in which you give notifications to the user regarding any particular error or any sort of message that you want.

So here's my basic view




Now all i want to display here is the text of the Textfield in my alert view, so i will go step wise here.

Step 1: Open Xcode and create a window based application and give it an appropriate name, i have given it AlertDemo
Step 2: Create a class of UIViewController type and create objects of label, button and a text field also we will require a function where we will write the code to display an Alert view, heres what the Myviewcontroller.h file looks like this

@interface Myviewcontroller : UIViewController {
UILabel *lbl;
UIButton *btn;
UITextField *txtfield;
}
-(void)buttonTouch; //function which will display alert view on button touch
@end

now coming to the Myviewcontroller.m file where we will initialize these objects and give logic to our view, the init method will look like this

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
// Custom initialization
//setting the label
lbl = [[UILabel alloc]initWithFrame:CGRectMake(33, 101, 49, 21)];
lbl.text = @"Name";
//setting the button
btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[btn setFrame:CGRectMake(119, 140, 72, 37)];//setting the frame
[btn setTitle:@"Touch" forState:UIControlStateNormal]; //setting the text and state of button

[btn addTarget:self action:@selector(buttonTouch) forControlEvents:UIControlEventTouchUpInside]; //calling the function
//setting the text
txtfield = [[UITextField alloc]initWithFrame:CGRectMake(90, 101, 130, 31)];
[txtfield setBorderStyle:UITextBorderStyleRoundedRect];
}
return self;
}

setting the buttonTouch method

-(void)buttonTouch
{
UIAlertView *objalert = [[UIAlertView alloc]initWithTitle:@"Alert box demo" message:txtfield.text delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:@"Cancel",nil];
[objalert show];
}

In the above code what i have done is created the object of UIAlertview, so now lets have a look at its parameters

1. initWithTitle: String that appears as the title of the message.
2.message: The initial message that you want to show to the user.
3. delegate: If you have specified some delegate to the alertview then set self else set it to nil.
4. cancelButtonTitle: The title of the cancel button else you can write nil if no cancel button.
5. otherButtonTitles: The title of another button.

Finally you write a nil in order to specify that their are no more button to be added to the alert view


now coming to the function called loadView,




- (void)loadView {
[super loadView];
[self.view addSubview:btn];
[self.view addSubview:txtfield];
[self.view addSubview:lbl];
}

Now we will go into the app delegate.m file and add the view to our window

#import "AlertDemoAppDelegate.h"
#import "Myviewcontroller.h"

@implementation AlertDemoAppDelegate

@synthesize window;

- (void)applicationDidFinishLaunching:(UIApplication *)application {

// Override point for customization after application launch
Myviewcontroller * obj = [[Myviewcontroller alloc]init];
[window addSubview:obj.view];
[window makeKeyAndVisible];
}


now after that you can touch the button and after that you can see the following result on the iPhone simulator.



BitCode hopes that this post has helped you in clearing your concepts regarding the view and view controllers. You can post your queries at bitcode.pune@gmail.com for any technical assistance.

Introduction To Xcode

In this post we shall have a look on how to add a UITextField onto the iPhone screen

UITextfield is a class which is belongs to the UIKit Framework it's just like your normal textbox, so now let's have a look at some of the basic functionality of UITextfield.

Open Xcode and select windows based application


After doing this give your project an appropriate name i have used textfielddemo as the name of our project

now from the groups and files section select classes and add UIViewController subclass file






After selecting UIViewController subclass a window will pop up whcih will ask you to save the file and give it an appropriate name which looks like this



I have given the name Myview now in the newely create file go to the Myview.h file thats the header file. Here you can see that just like in our earlier program we used to create a class which inherits from NSObject class here the class is already inherited from a class but this time its not NSObject its UIViewController.

difference between a UIView and a UIViewController,is that UIView is just a car with no engine and UIViewController is a car with engine means that UIView has no extra method which will make your coding easier but UIViewController has all those method which will make the coding part easier

Alright now coming to the coding part just open the Myview.h file and create an object of UITextfield class


now once you have declared the object of UITextfield now its time to use the object. We will go programatically that means not using the Interface builder.

First we will set the frame of text field and then set the boarder style and text of the text field. So now you select the .m file thats
Myview.m and add the follwing code in the init method

//allocate memory and set the frame for textfield object

obj_TextField = [[UITextField alloc]initWithFrame:CGRectMake(87, 135, 114, 31)];

[obj_TextField setBorderStyle:UITextBorderStyleRoundedRect]; //set the boarder style

obj_TextField.text = @"Radix"; //setting the text for textfield


and now coming to the load view function where we will add the textfield view. Now as a beginner you might see two functions here one which looks like this

- (void)loadView {

}

and the other which looks like this

- (void)viewDidLoad {

[super viewDidLoad];

}


well the only difference between load view and viewDidLoad function is that

loadView will load up your current view and assign it to the view property whereas viewDidLoad is a method which is called once the view has been loaded.

viewDidLoad method is called after loadView method and if you are making your application via code then use the loadView method else if you are using the Interface Builder then in that case use the viewDidLoad method.

Ok now coming back to our coding section in the loadView function

- (void)loadView {

[super loadView]; //loading super view

[self.view addSubview:obj_TextField]; //adding textfield object to the current view

[obj_TextField release]; //release the textfield object after display

}


In the above code you are calling [super loadView]; if you don't call it then what you are doing is that you are overriding the message without passing the message to the super object, in short the view of the textfield will not be loaded and your application will crash.

In the second line of code i have added the textfield object to my current view and in the last line i have released the textfield object as i won't be using it anymore

The entire code looks like this



Now in this section what you have did is that you have took a class called Myview and created an object of UITextField and gave it boarders and frame and added it to the view.

Now its time to display this view which is present in the
Myview class in your iPhone window. Since Myview is a class you have to make its object so lets see how this process is done, now you have to select the textfielddemoAppDelegate.m file and search for the method called applicationDidFinishLaunching which looks like this

- (void)applicationDidFinishLaunching:(UIApplication *)application {

// Override point for customization after application launch

[window addSubview:obj.view];

[window makeKeyAndVisible];

}


but before that you have to import the Myview.h header file as you will be creating the object of Myview class



alright now in this function you have to create the object of your class and then tell the window to show the view, hers the code for that

- (void)applicationDidFinishLaunching:(UIApplication *)application {

// Override point for customization after application launch

Myview *obj = [[Myview alloc]init]; //creating object of myview

[window addSubview:obj.view]; //adding the view to the window

[window addSubview:obj.view];

[window makeKeyAndVisible];

}


The entire code looks like this



now all you have to do now is run the application by clicking Build and Go inorder to run the iphone simulator and see the output which will be like this



BitCode hopes that this post has helped you in clearing your concepts regarding the view and view controllers. You can post your queries at bitcode.pune@gmail.com for any technical assistance.

Tuesday, 22 March 2011

UIView and UIViewController

In this post we will have a close look at the difference between UIView and UIViewController.

UIView and UIViewController both are classes and are of equally important but both the classes have their own specialty. First we will have a discussion of UIView and then UIViewController to understand the concepts of both the classes.

UIView: UIView is a class which holds the other views, in iPhone each and every control is refereed as a view. You can add a view inside another view and to hold all these views you need a container and your UIView class acts as container where you can add another views like the buttons,labels, switch, slider etc because of this the UIView can have the parent and child relationship between them know as the subviews and the parent view doing the embedding is called as the superview. Every UIView is related with UIViewController which manages the UIView.

Some Important instance method of UIView class

addSubview: It takes a parameter of the UIView instance (label,buttons, etc) which is to be added.

drawRect: This method is used when you want to draw something in the view like circle, rectange etc.

didAddSubView: Informs whether a subview was added.

removeFromSuperView: It removes a view from the parent view and from the responder chain.

UIViewController: UIViewController class is used to control the UIView instance (object) and the view which are present in the UIView (buttons,labels,table). The ViewController manages the modal view, device rotation, memory management etc, Subclasses such as UINavigationController and UITabbarController provide additional behavior for managing complex hierarchies of view controllers and views.The UIViewController has a view property with the help of which you can refer to the view which it controls, now this view can have another view like the button,label,table etc.

Some important instance method of the UIViewController class:


loadView: In this method you add the controls to the self view but before that you make a call to the super class loadview method so that the view that you are going to add in the self view are being added properly. This method is called when the view property of the viewController is accessed and you must use the loadView method only when you are adding views programmatically (without using Interface builder).

viewDidLoad: This method always gets called when we want do some manipulations with the views after they are loaded in the memory. This method is called regardless of whether the views were stored in a nib file or created programmatically in the loadView method.

viewDidAppear: Notifies the view controller that its view was added to a window.

viewDidDisappear: Notifies the view controller that its view was dismissed, covered, or otherwise hidden from view.

viewWillAppear: Notifies the view controller that its view is about to be become visible.

viewDidUnload: It is called when the when the controller’s view is released from the memory.


BitCode hopes that this post has helped you in clearing your concepts regarding the view and view controllers. You can post your queries at bitcode.pune@gmail.com for any technical assistance.


Foundation Framework

In this post we will have a look at the foundation framework and why it is used in all the iPhone projects.

You must be familiar with the header

 #import<Foundation/Foundation.h> 

The above line means that you will be using some of the classes from the Foundation Framework, The objective C foundation framework is a set of classes that are provided to ease the developers in developing the applications.

The foundation framework was developed by the organization called as the NEXT computers as a part of their NEXTStep environment. When Next was taken over by Apple Inc then the developers at Apple used Foundation framework as a part of the development for MAC OS X and after the success of MAC OS X they used it for the development for iPhone development kit.

Since the foundation framework took birth at NEXT all the classes which are present and in the foundation framework begin with the letters "NS".

While working with objective c you always inherit the class NSObject, the NSObject class is a part of the Foundation Framework and as you become more familiar with the framework you will learn that most Foundation classes are derived from this class. 

In order to know more about the foundation framework you can click here

BitCode hopes that this post has helped you in clearing your concepts regarding the foundation framework. You can post your queries at bitcode.pune@gmail.com for any technical assistance.