iHelloCodeBased Iapp Msmonir@
iHelloCodeBased Iapp Msmonir@
m
iHelloCodeBased - Hello to iPhone App using Code only
.c o
written by
ve
Mohammed Shirajum Monir
Dhaka, Bangladesh.
eli
web: http://msmonir.web.officelive.com
ffic
http://msmonir.tech.officelive.com
h.o
ec
on .
s m sh
ir.t
/m ade
htt Ba nly
o
p:/ ngl
de
.co ir; Dh g Co
k a,
eli on usin
or a
pp
m
eb iraju ne A
M
ve
ir.w Sh ho
.of m
on ed to iP
fic
/m am ello
p:/ oh - H
sm m
htt by M sed
a
we itten odeB
Wr lloC
iHe
b:
iHelloCodeBased Tutorial to develop iPhone App written by Mohammed Shirajum Monir Page 1
iHelloCodeBased Hello to iPhone App using Code only
m
In this tutorial I am going to explore the ability of Xcode with iPhone SDK to develop one iPhone App. The finished
.c o
product is going to be titled as iHelloCodeBased, as displayed in the cover. It is to be noted that I'm not using the
Interface Builder, instead, I am going to build using pure code. The idea behind is to show that interface builder isn't
the only solution to work.
ve
eli
Requirements: One computer where the iPhone SDK is installed. iPhone SDK includes- Xcode, iPhone Simulator, and
Interface Builder.
ffic
h.o
Lets get started, if you have confirmed the requirements stated above...
When you launch Xcode you'll be presented with a welcome screen. You can either look through that or just dismiss it,
ec
on .
none of it is particularly important. What we need is a new project.
s m sh
ir.t
/m ade
1. Start Xcode. Xcode is usually found at the Developer folder.
htt Ba nly
2. Select File > New Project to bring up the project templates.
3. Make sure to select Window-Based Application from the displayed templates as shown in fig-01.
o
p:/ ngl
de
.co ir; Dh g Co
k a,
eli on usin
or a
pp
m
eb iraju ne A
M
ve
ir.w Sh ho
.of m
on ed to iP
fic
/m am ello
p:/ oh - H
sm m
htt by M sed
[Fig-01]
4. Clicking the Choose,,, button would ask you to enter a name for your iPhone App. I named it as as shown in Fig-02.
a
we itten odeB
Wr lloC
iHe
b:
iHelloCodeBased Tutorial to develop iPhone App written by Mohammed Shirajum Monir Page 2
iHelloCodeBased Hello to iPhone App using Code only
m
.c o
ve
eli
ffic
h.o
ec
on .
s m sh
ir.t
/m ade
htt Ba nly
o
p:/ ngl
de
.co ir; Dh g Co
k a,
eli on usin
a
Fig-02
5. Once the project is created, you'll be presented with the Xcode interface and all of the files the project template has
or
m
eb iraju ne A
M
ve
ir.w Sh ho
.of m
on ed to iP
fic
/m am ello
p:/ oh - H
sm m
htt by M sed
a
we itten odeB
Wr lloC
iHe
Fig-03
b:
iHelloCodeBased Tutorial to develop iPhone App written by Mohammed Shirajum Monir Page 3
iHelloCodeBased Hello to iPhone App using Code only
m
6. Now click on iHelloCodeBasedAppDelegate.m to open. We would find one function named
.c o
applicationDidFinishLaunching as below:
- (void)applicationDidFinishLaunching:(UIApplication *)application {
ve
// Override point for customization after application launch
[window makeKeyAndVisible];
eli
}
This is the function where we have to create a view controller to hold a label in which I am going to write something.
ffic
So, Right mouse click on the Classes folder and choose Add > New File.
7. Make sure you have selected UIViewControllersubclass, and then click the next button, as displayed at Fig-04. I
h.o
named it as iHelloCodeBasedViewController.m.
ec
on .
s m sh
ir.t
/m ade
htt Ba nly
o
p:/ ngl
de
.co ir; Dh g Co
k a,
eli on usin
or a
pp
m
eb iraju ne A
M
ve
ir.w Sh ho
.of m
on ed to iP
fic
Fig-04
/m am ello
8. Let us examine iHelloCodeBasedViewController.m. We would find several commented lines in it. To manually
populate a view controller, we have to override the loadView method. So, uncomment these following lines found on
the mentioned file as below:
p:/ oh - H
- (void)loadView {
sm m
9. We need to create a UIView object that is the size of our display and set it to the view controllers view property. I
htt by M sed
am going to accomplish this by adding some lines into - (void)loadView {} method. So, this method would look like
this:
a
we itten odeB
- (void)loadView
{
CGRect msmFrame=CGRectMake(0, 0, 320, 480); // frame sets the bounds of the viewas the screen is 320x480
self.view=[[UIView alloc] initWithFrame:msmFrame]; // allocsting the view
self.view.backgroundColor=[UIColor blackColor]; // setting the background color
msmFrame=CGRectMake(10, 25, 300, 300); // setting the label with size and position in the frame
Wr lloC
[lblHello release]; // destroy it. Objective-C uses reference counting to automatically delete objects,
so whenever you're done with a reference, you need to call release.
b:
iHelloCodeBased Tutorial to develop iPhone App written by Mohammed Shirajum Monir Page 4
iHelloCodeBased Hello to iPhone App using Code only
m
10. We have just created one view controller for our application. But, now its time to create an instance of it and attach
.c o
into our application. How can we? Don't worry its easy task. all we have to do is work with the function
applicationDidFinishLaunching which can be found at iHelloCodeBasedAppDelegate.m. So... what are we waiting
for? First, to make the compiler be able to be familiar with the object, iHelloCodeBasedViewController, we have
ve
created, for this, add one import statement atjust after the existing import statement found on the file
iHelloCodeBasedAppDelegate.m. Thus only two import statements would be found as shown below:
eli
#import "iHelloCodeBasedAppDelegate.h"
#import "iHelloCodeBasedViewController.h"
ffic
11. Now its time to create the view controller. modify the applicationDidFinishLaunching function as below:
h.o
- (void)applicationDidFinishLaunching:(UIApplication *)application {
self.viewController=[iHelloCodeBasedViewController alloc]; // allocate the view
[window addSubview:self.viewController.view]; // sdding viewController as sub-view
ec
// Override point for customization after application launch
on .
[window makeKeyAndVisible];
s m sh
ir.t
}
12. Almost done. I am required to set some properties, usually at AppDelegate.h. So, lets modify
/m ade
htt Ba nly
iHelloCodeBasedAppDelegate.h so that it would look like this:
//
o
p:/ ngl
// iHelloCodeBasedAppDelegate.h
// iHelloCodeBased
//
// de
Created by Mohammed Shirajum Monir (Monir) on 6/3/10.
.co ir; Dh g Co
// Copyright http://msmonir.web.officelive.com 2010. All rights reserved.
// a,
#import <UIKit/UIKit.h>
k
@class iHelloCodeBasedViewController; // this line is added
eli on usin
a
@interface iHelloCodeBasedAppDelegate : NSObject <UIApplicationDelegate>
{
UIWindow *window;
iHelloCodeBasedViewController *viewController; // this line is added
or
}
pp
@end
M
ve
13. Now the crucial parts. In iHelloCodeBasedAppDelegate.m we have to use @synthesize and dealloc. First add
ir.w Sh ho
.of m
the following line just after the available line @synthesize window;.
on ed to iP
@synthesize viewController;
- (void)dealloc
/m am ello
{
[viewController release];
[window release];
[super dealloc];
p:/ oh - H
}
sm m
htt by M sed
15. All codes are finished. Its the time to have fun. Yeah... just click the button labelled Build and Go in
Xcode. Have you found your first iPhone Application which is completely code based as shown below in Fig-05?
a
we itten odeB
Wr lloC
iHe
b:
iHelloCodeBased Tutorial to develop iPhone App written by Mohammed Shirajum Monir Page 5
iHelloCodeBased Hello to iPhone App using Code only
m
.c o
ve
eli
ffic
h.o
ec
on .
s m sh
ir.t
/m ade
htt Ba nly
o
p:/ ngl
de
.co ir; Dh g Co
k a,
eli on usin
or a
pp
m
eb iraju ne A
M
ve
ir.w Sh ho
.of m
on ed to iP
fic
/m am ello
p:/ oh - H
sm m
htt by M sed
Fig-05
a
You can find the tutorial and full source code built throughout this tutorial here iHelloCodeBased.zip.
we itten odeB
b:
iHelloCodeBased Tutorial to develop iPhone App written by Mohammed Shirajum Monir Page 6