Tuesday 29 March 2011

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.

1 comment: