ihellocodebased iapp [email protected]

Upload: engineer-mohammed-shirajum-monir

Post on 30-May-2018

219 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/9/2019 iHelloCodeBased iApp [email protected]

    1/6

    iHelloCodeBased Hello to iPhone App using Code only

    iHelloCodeBased Tutorial to develop iPhone App writ ten by Mohammed Shirajum Monir Page 1

    iHelloCodeBased - Hello to iPhone App using Code only

    written by Mohammed Shirajum Moni rDhaka, Bangladesh.

    web: http://msmonir.web.officelive.comhttp://msmonir.tech.officelive.com

  • 8/9/2019 iHelloCodeBased iApp [email protected]

    2/6

    iHelloCodeBased Hello to iPhone App using Code only

    iHelloCodeBased Tutorial to develop iPhone App writ ten by Mohammed Shirajum Monir Page 2

    In this tutorial I am going to explore the ability of Xcode with iPhone SDK to develop one iPhone App. The finishedproduct is going to be titled as iHelloCodeBased , as displayed in the cover. It is to be noted that I'm not using theInterface Builder, instead, I am going to bu ild using pure code. The idea behind is to show that interface builder isn'tthe only solution to work.

    Requirements: One computer where theiPhone SDKis installed.iPhone SDKincludes- Xcode, iPhone Simulator, andInterface Builder.

    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,

    none of it is particularly important. What we need is a new project.

    1. Start Xcode. Xcode is usually found at the Developer folder.2. Select File > New Projectto bring up the project templates.

    3. Make sure to select Window-Based Application from the displayed templates as shown in fig-01.

    [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.

    http://www.apple.com/iPhonehttp://www.apple.com/iPhonehttp://www.apple.com/iPhonehttp://www.apple.com/iPhonehttp://www.apple.com/iPhonehttp://www.apple.com/iPhonehttp://www.apple.com/iPhonehttp://www.apple.com/iPhone
  • 8/9/2019 iHelloCodeBased iApp [email protected]

    3/6

    iHelloCodeBased Hello to iPhone App using Code only

    iHelloCodeBased Tutorial to develop iPhone App writ ten by Mohammed Shirajum Monir Page 3

    Fig-02

    5. Once the project is created, you'll be presented with the Xcode interface and all of the files the project template hasgenerated for you, as shown in Fig-03

    Fig-03

  • 8/9/2019 iHelloCodeBased iApp [email protected]

    4/6

    iHelloCodeBased Hello to iPhone App using Code only

    iHelloCodeBased Tutorial to develop iPhone App writ ten by Mohammed Shirajum Monir Page 4

    6. Now click on iHelloCodeBasedAppDelegate.m to open. We would find one function namedapplicationDidFinishLaunching as below:

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

    // Override point for customization after application launch

    [windowmakeKeyAndVisible];

    }

    This is the function where we have to create a view controller to hold a label in which I am going to write something.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. Inamed it as iHelloCodeBasedViewController.m.

    Fig-04

    8. Let us examine iHelloCodeBasedViewController.m. We would find several commented lines in it. To manuallypopulate a view controller, we have to override the loadView method. So, uncomment these following lines found on

    the mentioned file as below:- (void)loadView {

    }

    9. We need to create a UIView object that is the size of our display and set it to the view controllers view property. Iam going to accomplish this by adding some lines into - (void)loadView {} method. So, this method would look likethis:

    - (void)loadView

    {

    CGRect msmFrame=CGRectMake(0, 0, 320, 480); // frame sets the bounds of the viewas the screen is 320x480

    self.view=[[UIViewalloc] initWithFrame:msmFrame]; // allocsting the view

    self.view.backgroundColor=[UIColorblackColor]; // setting the background color

    msmFrame=CGRectMake(10, 25, 300, 300); // setting the label with size and position in the frame

    UILabel *lblHello=[[UILabelalloc] initWithFrame:msmFrame]; // alocate a label

    lblHello.text=@"Congratulations!!! You have done it. For more tutorials visit:

    http://msmonir.web.officelive.com";

    [self.view addSubview:lblHello]; // adding the label into the view

    [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.

    }

  • 8/9/2019 iHelloCodeBased iApp [email protected]

    5/6

    iHelloCodeBased Hello to iPhone App using Code only

    iHelloCodeBased Tutorial to develop iPhone App writ ten by Mohammed Shirajum Monir Page 5

    10.We have just created one view controller for our application. But, now its time to create an instance of it and attachinto our application. How can we? Don't worry its easy task. all we have to do is work with the functionapplicationDidFinishLaunching which can be found at iHelloCodeBasedAppDelegate.m . So... what are we waitingfor? First, to make the compiler be able to be familiar with the object, iHelloCodeBasedViewController, we havecreated, for this, add one import statement atjust after the existing import statement found on the fileiHelloCodeBasedAppDelegate.m . Thus only two import statements would be found as shown below:

    #import "iHelloCodeBasedAppDelegate.h"

    #import "iHelloCodeBasedViewController.h"

    11.Now its time to create the view controller. modify the applicationDidFinishLaunching function as below:

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

    self.viewController=[iHelloCodeBasedViewControlleralloc]; // allocate the view

    [windowaddSubview:self.viewController.view]; // sdding viewController as sub-view

    // Override point for customization after application launch

    [windowmakeKeyAndVisible];

    }

    12.Almost done. I am required to set some properties, usually at AppDelegate.h. So, lets modifyiHelloCodeBasedAppDelegate.h so that it would look like this://

    // iHelloCodeBasedAppDelegate.h

    // iHelloCodeBased

    //

    // Created by Mohammed Shirajum Monir (Monir) on 6/3/10.

    // Copyright http://msmonir.web.officelive.com 2010. All rights reserved.

    //

    #import

    @classiHelloCodeBasedViewController; // this line is added

    @interface iHelloCodeBasedAppDelegate : NSObject

    {

    UIWindow *window;

    iHelloCodeBasedViewController *viewController; // this line is added

    }

    @property (nonatomic, retain) IBOutletUIWindow *window;

    @property (nonatomic, retain) iHelloCodeBasedViewController *viewController; // this line is added

    @end

    13.Now the crucial parts. In iHelloCodeBasedAppDelegate.m we have to use @synthesize and dealloc. First addthe following line just after the available line @synthesize window;.

    @synthesize viewController;

    14.Last of all modify the dealloc as this:

    - (void)dealloc

    {

    [viewControllerrelease];

    [windowrelease];

    [superdealloc];}

    15.All codes are finished. Its the time to have fun. Yeah... just click the button labelled Build and Go inXcode. Have you found your first iPhone Application which is completely code based as shown below in Fig-05?

  • 8/9/2019 iHelloCodeBased iApp [email protected]

    6/6

    iHelloCodeBased Hello to iPhone App using Code only

    iHelloCodeBased Tutorial to develop iPhone App writ ten by Mohammed Shirajum Monir Page 6

    Fig-05

    You can find the tutorial and full source code built throughout this tutorial hereiHelloCodeBased.zip.

    I hope you have enjoyed it. See you soon.

    http://msmonir.tech.officelive.com/iPhone/iPhoneAppTutorial/iHelloCodeBased_Tutorial_Code/iHelloCodeBased.ziphttp://msmonir.tech.officelive.com/iPhone/iPhoneAppTutorial/iHelloCodeBased_Tutorial_Code/iHelloCodeBased.ziphttp://msmonir.tech.officelive.com/iPhone/iPhoneAppTutorial/iHelloCodeBased_Tutorial_Code/iHelloCodeBased.ziphttp://msmonir.tech.officelive.com/iPhone/iPhoneAppTutorial/iHelloCodeBased_Tutorial_Code/iHelloCodeBased.zip