Saturday, April 30, 2011

Introduction of Navigation based Application in iPhone

In iPhone programming, navigation based application helps us to make application which contains many screens those should me connected to each other and they should be easily navigated to other screens. iOS sdk provides us Navigation Controller for make it easy.


Best primary example of navigation based application is Contact Application in iPhone. Such application can be started to build by selecting Navigation Based Application in Xcode.


When you create navigation based applications, you will find many files made by Xcode them self. Each file has its own importance. 


 You Should find following files after creating navigation application.


  • CoreGraphics.framework, Foundation.framwork, UIKit.framework - These are the basic libraries which are provided by apple. These library contains many functions as other languages.
  • HelloWorld.app -  This is an Application which will be installed in iPhone
  • Hello_World_Prefix.pch -   It contains some code to include the data inside the frameworks.
  • Hello_WorldAppDelegate.h -This is a header file that contains all of our definitions for variables.  It is very similar to a header file in other languages.
  • Hello_WorldAppDelegate.m -   This is first execution file which will be executed when program runs. The main .m file calls this object.
  • Info.plist - This contains various meta information about our program.  
  • main.m - Like most programming language, this file contains our main function. Execution begins from here.  The main function basically instantiates our object and starts the program.  We should not need to edit this file.
  • MainWindow.xib - This contains the visual information of  main window.  If we double click on it, it will open in a program called “Interface Builder”.
  • RootViewController.h, RootViewController.m - These are files for a view controller that gets added to our main window.  
  • RootViewController.xib - This is a display file which can be edited in interface builder.
We will write our main code of Hello World in Root View Controller.m file.
Open that file and write following code.


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 1;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *Identifier = @"MyIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:Identifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:Identifier] autorelease];
}
[cell setText:@"Hello World"];
// Set up the cell
return cell;
}


 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
// Navigation logic
}


Save and Compile your program. Run the program in simulator. 

You should get following output.


No comments:

Post a Comment