RSS

iPhone Dev Tips: Navigation and Tap Bar Controller

27 apr

1. UINavigationController

Handling navigation controllers programmatically is very easy. Starting from scratch and without using IB, you can create a navigation controller in the applicationDidFinishLaunching delegate medhod. Next you need to add it at least one view controller. Adding more controllers results in displaying only the last one which resides at the top of the hierarchy. Last you need to add the navigation controller’s view to the window. Like this:

- (void)applicationDidFinishLaunching:(UIApplication *)application 
{
   navController=[[UINavigationController alloc] init];
   MyViewController *firstController=[[MyViewController alloc] init];
   [navController pushViewController:firstController animated:NO];
   [window addSubview navController.view];
} 
- (void) dealloc
{
   ...  
   [navController release];
   ...
}

2. Customizing navigation controller’s bar

The navigation controller’s bar is customized by the top (visible) view controller. Each view controller can customize left, title or right view. The right place to implement the code is viewDidLoad. Specifically you create a UIBarButtonItem with a customized or system style, a target (usually the view controller itself) and an action. Don’t forget to release the newly created bar button after you assigned it to the navigation item.

Some system defined bar buttons trigger specific action. The Edit Bar Button, if pressed, triggers a selector in which you can change the user interface and set it ready for editing. The caption changes to Done.

You can customize the titleView by adding a view of yours. 

If the selection of an item triggers the push of a second view controller onto the first one, the left button automatically becomes the back button displaying the title of the first view controller. There are times when you want to change the label to something shorter. You can do this (in the first view controller) by using the property navigationItem.backButtonItem.

-(void) viewDidLoad
{
   UIBarButtonItem *myButton=[[UIBarButtonItem alloc]
      initWithTitle:@"my" style:UIBarButtonItemStyleBordered
      target:self action:@selector(doSomething:)];  
   UIBarButtonItem *myBackButton=[[UIBarButtonItem alloc]
      initWithTitle:@"back" style:UIBarButtonItemStyleBordered
      target:nil action:nil];  

self.navigationItem.leftBarButtonItem = myButton;
self.navigationItem.rightBarButtonItem = self.editButtonItem;  
self.navigationItem.titleView = myView; // or self.title=@"My View";
self.navigationItem.backButtonItem=myBackButton;  [myButton release];
[myBackButton release];
}

- (void) setEditing:(BOOL) editing animated:(BOOL) animated
{
if( editing == YES ) myTextField.enabled=YES;

3. AlertView

A fast easy way to display a classical modal dialog box. 

   UIAlertView *myView=[[UIAlertView alloc] initWithTitle:@"Hey" message:@"Watch out" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];

4. Tap Bar Controller

Tap bar controllers are defined programmatically just like navigation controllers, usually in the applicationDidFinishLaunching delegate method. The title and image of each tap item is defined in its bound view controller by means of its property tapBarItem.

 

 

- (void)applicationDidFinishLaunching:(UIApplication *)application 
{
   tapBarController=[[UITapBarController alloc] init];
   MyFirstViewController *first=[[MyFirstViewController alloc] init];
   MySecondViewController *second=[[MySecondViewController alloc] init];
   tapBarController.viewControllers = [NSArray arrayWithObjects:first, second, nil];
   [window addSubview tapBarController.view];
} 
 

-(void) viewDidLoad
{
   self.tapBarItem = [[UITapBarItem alloc] initWithTitle:@"People" image:someImage tag:0];
}

5. Put them altogether

You can have any kind of view hierarchy you want by nesting navigation controllers into tap bar controllers. 

 

   tapBarController=[[UITapBarController alloc] init];
   MyFirstViewController *first=[[MyFirstViewController alloc] init];
   MySecondViewController *second=[[MySecondViewController alloc] init];
   navController=[[UINavigationController alloc] init];
   [navController pushViewController:second animated:NO];
   tapBarController.viewControllers = [NSArray arrayWithObjects:first, navController, nil];
}

 

 

About these ads
 
Lascia un commento

Pubblicato da su aprile 27, 2009 in Mac OS X, Tutorial

 

I commenti sono chiusi.

 
Iscriviti

Ricevi al tuo indirizzo email tutti i nuovi post del sito.

%d blogger cliccano Mi Piace per questo: