26 December, 2009

Developing application using Cocoa Touch.

Cocoa Touch comprises of the UIKit and Foundation framework.
Design Patterns:
The main patterns that you will use in simple cocoa touch based application are:
  1. Delegation
  2. Model View Controller
  3. Target Action
Delegation : Delegation is pattern where one object periodically sends messages to another object specified as its delegate, to notify the delegate that an event is occurring.
Example : In a normal application, application object tells its delegate(which is AppDelegate class) that the main start-up routine have finished and that the custom configuration can begin(to create an instance of a controller to set up and manage the view).
Model View Controller : MVC sets out 3 roles for objects in an application
Model : Model object represents data.
Example : 
  1. Space ship and rockets in a game.
  2. Todo items in a productive application.
  3. Circle and squares in a drawing application.
View : View answers for the question "How to display the data?", may allow the user to edit the data.
Controller : Controller object mediates between models and views.
Target Action :
The target-action mechanism enables a control object - that is, an object such as a button or slider - in response to a user event (such as a click or a tap), sends a message(the action) to another object (the target) that can interpret the message and handle it as an application specific instructions.


Xcode : Apple's IDE(Integrated development Environment).


Info.plist : Info.plist is a dictionary that contains information about the application such as its name and icon.


Bundle : 
A Bundle is an abstraction of a location in the file system that groups code and resources that can be used in an application.


Forward declaration in Objective C:
This is a promise to the compiler that class will be defined some where else and that it needn't waste time checking for it now.
Forward declaration in Objective C : @class MyViewController.


Nib files : Nib files contains an archive of user interface elements (file extension will be .xib).Clicking on it opens the xib file in Interface builder.
NIB : NextStep Interface Builder


Interface Builder : 
This application is used to create user interfaces. It does not create/generate source code, instead it allows you to manipulate object directly and then save those objects in an archive called nib files.
At runtime, when nib is loaded the objects are unarchived and restored to the state they were in when you saved the file including connection between them.
The Interface Builder document contains 4 items:
  1. File's owner proxy object.
  2. File's responder proxy object.
  3. AppDelegate.
  4. A Window.
Files Owner Proxy Object : The files owner object is not created when the nib file is loaded, instead it is the object that loads the nib file.You have to mention the class of object that loads the nib in the identity inspector.It will automatically set the class name if you create the together with the view controller class.
The View Outlet : An outlet is just an attribute (typically an instance variable) that happens to connect to an item in a nib file.The outlet connection means that when the nib file is loaded and the UIView instance is unarchived, the view controller view instance variable is set to that view.
Note : You can look at - and make and break - an object's connection using an inspector panel.In interface builder document-click on files owner to display a translucent panel showing file's owner connections.


Application Bootstrapping:
If you create a project using template provided by Xcode already sets up the basic application environment.
It creates:
  1. Application object.
  2. Connects to the window server.
  3. Establishes the run loop and so on.
Note : Most of the work is done by UIApplicationMain function.
main function which is normally available in main.m is the starting point to iPhone application.
When user taps on the application then UIApplication looks at info.plist and loads the main lib file(file name that is associated with NSMainNibFile key in that plist). And following things will happen
  1. STEP 1 : Delegate creates view controller object and initializes it.
  2. STEP 2 : The Delegate asks its view controller for its view.
  3. STEP 3 : Finally delegate adds that view as a subview of the window.
Note : Xcode template provides an application delegate class and an instance is created in the nib file.Finally our responsibility is to implement a view controller class and create an instance of it.
Example code for each step:

STEP 1 : Delegate creates view controller object and initializes it:
view controller will be initialized with intWithNibName:bundle: method.
MyViewController* aViewController = [[MyViewController alloc] initWithNibName:@"MyViewController" bundle:[NSBundle mainBundle]];
[self setMyViewController:aViewController];
[aViewController release];
STEP 2 : The Delegate asks its view controller for its view.
UIView* controllerView = [myViewController view];
STEP 3 : Delegate adds that view as a subview of the window.
[window addSubView : controllerView];
View Controller Class:
View controller object plays a central role in most iPhone applications.This is responsible for managing a view.
UIKit provides a special class - UIViewController - that encapsulates most of the default behavior you want from a view controller.You have to create a subclass to customize your behavior for your application.
Note : The View controller lasts for the life time of the application, so it is good practice to add it as an instance variable of the application delegate.


Disabling Status bar:

Added "Status bar is initially hidden" check box to info.plist and check the check box this will disables the status bar.






IBOutlet : IBOutlet is a special keyword that is used only to tell Interface Builder to treat an instance variable or property as an outlet. It’s actually defined as nothing so it has no effect at compile time.
Example:


UILabel *label;
@property (monatomic, retain) IBOutlet UILable* label;

IBAction  : IBAction is a special keyword that is used only to tell Interface Builder to treat a method as an action for target/action connections. It’s defined to void.
Example:


- (IBAction)changeGreeting:(id)sender;
Getting access to AppDelegate instance :
Use following code to get access to AppDelegate:


[[UIApplication sharedApplication] delegate]


iPhone OS Graphics Overview:
      Core Animation is fundamental to the iPhone graphics sub-system.
      Every UIView object in your application is backed by a Core Animation layer.As various layers update their contents, they are animated and composited by Core Animation and presented to the display.
      OpenGL ES is also a client of Core Animation.To use OpenGL ES to draw to the screen, your application creates a UIView class backed by a special Core Animation layer, CAEAGLLayer object.
      CAEAGLLayer : A CAEAGLLayer object is aware of OpenGL ES and can be used to create rendering targets that acts as part of CoreAnimation.
Note: In your application you can create screens using both OpenGL ES layer and non-OpenGL ES layer drawing , but this hits performance.


No comments:

Post a Comment