UINavigationController provides a dynamic stack of views created at run time with a navigation bar at the top. For many applications this is used to provide all of the views the user sees.
Useful Documentation
Adding To A Project
Create an App as normal with the main view XIB
Create at least 1 view you will want to display (see Adding A New View below)
Add to #AppDelegate.h
(#AppDelegate_iPhone.h or #AppDelegate_iPad.h for multi version apps, not #AppDelegate_Shared.h)
For a multi version app
@class #ViewController; //- ADD THIS (the default ViewController file to show)
@interface AppDelegate_iPhone : AppDelegate_Shared
{
#ViewController *#ViewController1; //- ADD THIS
For a single version app
@class #ViewController; //- ADD THIS (the default ViewController file to show)
@interface AppDelegate_Shared : NSObject
{
UIWindow *window;
#ViewController *#ViewController1; //- ADD THIS
Add to #AppDelegate.m application didFinishLaunchingWithOptions
(Again, in #AppDelegate_iPhone.m or #AppDelegate_iPad.m for multi version apps)
//*******************************************************
//*******************************************************
//********** DID FINISH LAUNCHING WITH OPTIONS **********
//*******************************************************
//*******************************************************
//Create a ItemsViewController
#ViewController1 = [[#ViewController alloc] init];
//Create an instance of a UINavigationController
UINavigationController *navController = [[UINavigationController alloc]
initWithRootViewController:#ViewController1]; //The default view to show #ViewController
//Place ItemsViewController table view in the window hierarchy
[window addSubview:[navController view]];
[self.window makeKeyAndVisible];
Add the ViewDidLoad method to #ViewController.m
Each view will have a top navigation bar so we need to set it up
//***********************************
//***********************************
//********** VIEW DID LOAD **********
//***********************************
//***********************************
- (void)viewDidLoad
{
[super viewDidLoad];
//----- SETUP NAVITATION BAR FOR THIS VIEW -----
//[[self navigationItem] setLeftBarButtonItem:[self editButtonItem]]; //Display a default edit button (if you want this)
[[self navigationItem] setTitle:@"Current Reminders"]; //Title text
return;
}
Adding A New View
Menu > File > New File iOS > Cocoa Touch Class > UIViewControllerSubClass
With XIB for user interface = checked
Name it (a good idea to use format #ViewController and add _iPhone or _iPad to the end for multi version apps, for example MySub1ViewController_iPhone.m)
Design the view as required.
Tasks In The New View
In MySub1ViewController.m
//
Setting title in say the viewDidLoad
method//Set navigation bar to display name [[self navigationItem] setTitle:[editingPossession possessionName]];
To Deal With When View Closes
Add view will disappear method
//*****************************************
//*****************************************
//********** VIEW WILL DISAPPEAR **********
//*****************************************
//*****************************************
- (void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
//Clear first responder (in case any input objects are first responder)
[SomeTextField1 resignFirstResponder];
[SomeTextField2 resignFirstResponder];
}
Close This View
[[self navigationController] popViewControllerAnimated:(BOOL)YES];
Causing A View To Display
In the view that will cause this view to be displayed (e.g. the MyMainViewController in this example)
In MyMainViewController.h
@class MySub1ViewController; //-ADD THIS
@interface MyMainViewController : UITableViewController
{
MySub1ViewController *mySub1ViewController; //-ADD THIS
//Add this:
//### Remember to release any view objects you add in viewDidUnload and dealloc ###
In MyMainViewController.m
#import "MySub1ViewController.h"
//In the function where you want to display the new view
//Need to create an instance of MySub1ViewController?
if (!mySub1ViewController)
mySub1ViewController = [[MySub1ViewController alloc] init];
//Push it to the top of the navigation controllers stack
[[self navigationController] pushViewController:mySub1ViewController
animated:YES];