Saturday, January 22, 2011

iphone first Application


The main tool you use to create applications for iOS is Xcode—Apple’s IDE (integrated development environment). You can also use it to create a variety of other project types, including Cocoa and command-line utilities.

>> Launch Xcode (by default it’s in /Developer/Applications), then create a new project by choosing File > New Project. You should see a new window similar to this:

>> Select the Window-Based Application.

>> In the Product popup menu, make sure iPhone is selected. (Do not select the option to use Core Data for storage. You don’t use Core Data in this example.)

>> Click Choose.


A sheet appears to allow you to select where your project will be saved.

>> Select a suitable location for your project (such as the Desktop or a custom Projects directory), then give the project a name—HelloWorld—and click Save.

You see like this :


If you haven’t used Xcode before, take a moment to explore the application. You should read Xcode Workspace Guide to understand the organization of the project window and how to perform basic tasks like editing and saving files. You can now build and run the application to see what the Simulator looks like.


>> Choose Build > Build and Go (Run) or click the Build and Go button in the toolbar.


The iPhone Simulator application should launch automatically, and you need to understand how the application starts up.

>> Quit the Simulator.

Application Bootstrapping



   
The main function in main.m calls the UIApplicationMain function:
    int retVal = UIApplicationMain(argc, argv, nil, nil);

This creates an instance of UIApplication. It also scans the application’s Info.plist property list file. The Info.plist file is a dictionary that contains information about the application such as its name and icon. It may contain the name of the nib file the application object should load, specified by the NSMainNibFile key. Nib files contain an archive of user interface elements and other objects—you’ll learn more about them later in the tutorial. In your project’s Info.plist file you should see:
    NSMainNibFile
    MainWindow

the MainWindow nib file is loaded.

>> To look at the nib file, double-click MainWindow.xib in the Resources group in the project window (the file has the extension “xib” but by convention it is referred to as a “nib file”). Interface Builder launches and opens the file.



The Interface Builder document contains four items:
·         A File’s Owner proxy object. The File’s Owner object is actually the UIApplication instance—File’s Owner is discussed later, in “File’s Owner.”
·         A First Responder proxy object. The First Responder is not used in this tutorial but you can learn more about it by reading “Event Handling” in iOS Application Programming Guide.
·         An instance of HelloWorldAppDelegate set to be the application's delegate. Delegates are discussed in the next section.
·         A window. The window has its background set to white and is set to be visible at launch. It’s this window that you see when the application launches.

After the application has finished launching, you can perform additional customization


Adding a View Controller Class

>> In Xcode, in the project organizer select either the project (HelloWorld at the top of the Groups and Files list) or the Classes group folder—the new files will be added to the current selection.

>> Choose File > New File and in the New File window. Select the Cocoa Touch Classes group, then select UIViewController subclass. In the Options section, choose only With XIB for user interface.

 
Selecting “With XIB for user interface” means that Xcode creates a nib file to accompany the view controller, and adds it to the project.


>> Click Next, and in the following screen give the file a new name such as MyViewController (by convention, class names begin with a capital letter). Make sure that both the .m and .h files are created and that the files marked as being added to the project’s target, as shown here:


>> Press Finish and make sure that the files were added to your project.

Adding a View Controller Property

>> In the application delegate’s header file (HelloWorldAppDelegate.h), add this forward declaration before the interface declaration for HelloWorldAppDelegate:
@class MyViewController;

>> Add the instance variable by adding the following line between the braces:
MyViewController *myViewController;

>> Add a declaration for this property after the closing brace but before @end:
@property (nonatomic, retain) MyViewController *myViewController;
Properties are described in the “Declared Properties”.

To make sure you’re on track, confirm that your HelloWorldAppDelegate class interface file (HelloWorldAppDelegate.h) looks like this (comments are not shown):
#import 
 
@class MyViewController;
 
@interface HelloWorldAppDelegate : NSObject  {
    UIWindow *window;
    MyViewController *myViewController;
}
 
@property (nonatomic, retain) IBOutlet UIWindow *window;
@property (nonatomic, retain) MyViewController *myViewController;
 
@end
You can now create an instance of the view controller.

Creating the View Controller Instance

>> In the implementation file for the application delegate class (HelloWorldAppDelegate.m), create an instance of MyViewController by adding the following code as the first statements in the implementation of the application:didFinishLaunchingWithOptions: method:
MyViewController *aViewController = [[MyViewController alloc]
            initWithNibName:@"MyViewController" bundle:[NSBundle mainBundle]];
[self setMyViewController:aViewController];
[aViewController release];
There’s quite a lot in just these three lines. What they do is:
·         Create and initialize an instance of the view controller class.
·         Set the new view controller to be the myViewController instance variable using an accessor method.
·         Remember that you didn’t separately declare setMyViewController:, it was implied as part of the property declaration—see “Adding a View Controller Property.”
·         Adhere to memory management rules by releasing the view controller.
create the view controller object using alloc, then initialize it using initWithNibName:bundle:. The init method specifies first the name of the nib file the controller should load and second the bundle in which it should find it.

By convention, you own any objects you create using an alloc method (amongst others, see “Memory Management Rules”). You should also:
·         Relinquish ownership of any objects you create.
·         Typically use accessor methods to set instance variables anywhere other than in an initializer method.
The second line in the implementation uses an accessor method to set the instance variable, and then the third line uses release to relinquish ownership.

There are other ways to implement the above. You could, for example, replace the three lines with just two:
MyViewController *aViewController = [[[MyViewController alloc]
            initWithNibName:@"MyViewController" bundle:[NSBundle mainBundle]] autorelease];
[self setMyViewController:aViewController];
You could also replace the last line with:
self.myViewController = aViewController;

Setting Up the View

>> After releasing the view controller, add the following lines:
UIView *controllersView = [myViewController view];
[window addSubview:controllersView];

You could do this in one line:
[window addSubview:[myViewController view]];

The final line from the template:
[window makeKeyAndVisible];
 
>> In the implementation file for the application delegate class (HelloWorldAppDelegate.m), do the following:
·         At the top of the file, import the header file for MyViewController:
#import "MyViewController.h"
·         In the @implementation block of the class, tell the compiler to synthesize the accessor methods for the view controller:
@synthesize myViewController;
·         In the dealloc method, release the view controller in the first statement:
[myViewController release];

Implementation Source Listing

To make sure you’re still on track, confirm that your HelloWorldAppDelegate class implementation (HelloWorldAppDelegate.m) looks like this:
#import "MyViewController.h"
#import "HelloWorldAppDelegate.h"
@implementation HelloWorldAppDelegate
@synthesize window;
@synthesize myViewController;
 
- (BOOL)application:(UIApplication *)application
        didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    MyViewController *aViewController = [[MyViewController alloc]
                initWithNibName:@"MyViewController" bundle:[NSBundle mainBundle]];
    [self setMyViewController:aViewController];
    [aViewController release];
    UIView *controllersView = [myViewController view];
    [window addSubview:controllersView];
    [window makeKeyAndVisible];
}
 
- (void)dealloc {
    [myViewController release];
    [window release];
    [super dealloc];
}
@end

Test the Application

>> Compile and run the project (choose Build > Build and Run, or click the Build and Run button in Xcode’s toolbar).

Your application should compile without errors and you should again see a white screen in Simulator.











No comments:

Post a Comment