-
-
Save lamprosg/5024778 to your computer and use it in GitHub Desktop.
| //Create for every single view you're going to have a new view controller class | |
| //Subclass of UIViewController or UITableViewController if it's tableview based | |
| //With a correspoding .xib | |
| //Import the first view controller class that will be displayed in the .h file | |
| #import firstViewController.h | |
| //Creating outlet for the navigation controlle in the delegate .h file | |
| @property (strong, nonatomic) UINavigationController *navigationController; | |
| //OR (for the 2d way (look below) | |
| @property (strong, nonatomic) IBOutlet UINavigationController *navigationController; | |
| - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions | |
| { | |
| self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; | |
| // Override point for customization after application launch. | |
| /*****************************************************************************/ | |
| //If the view controller is a subclass of UITableViewController, there is no need to | |
| //make a nib file, It knows it's a tableview so you would write | |
| self.firstViewController = [[BIDFirstLevelViewController alloc] init]; | |
| //If you have a custom view | |
| //self.firstViewController = [[BIDFirstViewController alloc] initWithNibName:@"FirstViewController" bundle:nil]; | |
| //Instead of self.window.rootViewController =.. | |
| navigationController = [[UINavigationController alloc] initWithRootViewController:firstViewController]; | |
| [self.window addSubview:navigationController.view]; | |
| /*****************************************************************************/ | |
| //OR | |
| /*****************************************************************************/ | |
| //Create an empty .xib (named after the one in loadNibNamed:) | |
| //Change the File's Owner class of the xib to the delegate's one (ex. AppDelegate) | |
| //At the connection inspector find the outlet we created in the delegate's .h file and connect it | |
| //to the navigation Controller | |
| //We’re using a controller class provided by Apple (UINavigationController) instead of one we wrote ourselves. | |
| //See header file | |
| //Show the NavigatorController.xib | |
| [[NSBundle mainBundle] loadNibNamed:@"NavigatorController" owner:self options:nil]; | |
| //Set the root controller to the outlet we set pointing to the Tab bar controller (UITabBarController) | |
| self.window.rootViewController = self.navigationController; | |
| //Then you should edit the view controller's, inside the navigation controller, class and nib file | |
| //(at the identity and attribute inspectors) so it would load the view you want | |
| /*****************************************************************************/ | |
| self.window.backgroundColor = [UIColor whiteColor]; | |
| [self.window makeKeyAndVisible]; | |
| return YES; | |
| */ | |
| } |
| //The first view controller is going act as the data source and delegate for the table view | |
| //that displays the list of authors | |
| //Also import the second's view class | |
| #import "secondViewController.h" | |
| @interface BIDFirstViewController : UITableViewController | |
| //our data | |
| @property (nonatomic, strong) NSArray *authorList; | |
| //Declare an outlet to the second view controller and connect it | |
| //Open the current's (first) view .xib, find the outlet at the File's Owner | |
| //and drag it to the second View controller | |
| //CAUTION | |
| //You have to drag a view controller to the first view's .xib object area | |
| //and set the class of the secondViewController for it | |
| //This way you can connect the outlet to the second view controller | |
| property (nonatomic, strong) IBOutlet BIDSecondViewController *secondViewController; | |
| @end |
| #import "firstViewController.h" | |
| @implementation BIDFirstViewController | |
| - (void)viewDidLoad | |
| { | |
| [super viewDidLoad]; | |
| //Populate our data | |
| self.authorList = [[NSArray alloc] initWithObjects:@"Clancy, Thomas", @"Lehane, Dennis", nil]; | |
| //Set the title of the first view | |
| self.title = @"Authors"; | |
| } | |
| - (void)viewDidUnload | |
| { | |
| // Release anything that can be recreated in viewDidLoad or on demand. | |
| //Release the array if we don't need it | |
| self.authorList = nil; | |
| } | |
| /******************************************************************** | |
| When a table view is displayed, it needs to know how many sections and rows it is going to display. | |
| It finds this information by making calls to the numberOfSectionsInTable and numberOfRowsInSection methods | |
| ********************************************************************/ | |
| //Code for loading the table data | |
| - (NSInteger)numberOfSectionsInTableView: | |
| (UITableView *)tableView | |
| { | |
| // Return the number of sections. | |
| return 1; | |
| } | |
| - (NSInteger)tableView:(UITableView *) | |
| tableView numberOfRowsInSection:(NSInteger)section | |
| { | |
| // Return the number of rows in the section. | |
| return [self.authorList count]; | |
| } | |
| //Having identified the number of items to be displayed, | |
| //the table view then needs to get a UITableViewCell object containing the data to be displayed for each row. | |
| //It achieves this by calling the cellForRowAtIndexPath method for each row | |
| - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath | |
| { | |
| static NSString *CellIdentifier = @"Cell"; | |
| UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; | |
| if (cell == nil) | |
| { | |
| cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; | |
| } | |
| // Configure the cell. | |
| cell.textLabel.text = [self.authorList objectAtIndex:[indexPath row]]; | |
| return cell; | |
| } | |
| /******************************************************/ | |
| //Navigation code | |
| //Runs when the user selects something from the tableview | |
| - (void)tableView:(UITableView *)tableView | |
| didSelectRowAtIndexPath:(NSIndexPath *)indexPath | |
| { | |
| //For custom view | |
| self.secondViewController = [BIDSecondViewController alloc] initWithNibName:@"nibfilename" bundle:nil]; | |
| //For tableview (controller subclassed by UITableViewController) | |
| self.secondViewController = [BIDSecondViewController alloc] init]; | |
| //Set titles according to which row has been selected | |
| if (0 == indexPath.row) | |
| { | |
| self.secondViewController.title = @"Clancy"; | |
| } | |
| else | |
| { | |
| self.secondViewController.title = @"Lehane"; | |
| } | |
| [self.navigationController pushViewController:self.secondViewController animated:YES]; | |
| } | |
| @end |
| When developing a navigation-based application the central component of the architecture is the navigation controller. | |
| In addition, each screen has a view and a corresponding view controller. | |
| The navigation controller maintains a stack of these view controllers. When a new view is displayed it is pushed onto the | |
| navigation controller’s stack and becomes the currently active controller. The navigation controller automatically displays the | |
| navigation bar and the “back” button. When the user selects the button in the navigation bar to move back to the previous level, | |
| that view controller is popped off the stack and the view controller beneath it moved to the top becoming the currently active | |
| controller. | |
| The view controller for the first view that appears when the application is started is called the root view controller | |
| The first view controller cannot be popped off the navigation controller stack. | |
| The other view controllers further down are called subcontrollers | |
| ---------------------------------------------------------------------------------------------- | |
| Empty Application | |
| Create the first view controller (subclass UIViewController or UITableViewController if it's tableview based - our case). | |
| And corresponding view (.xib) | |
| Create a navigation controller in the appDelegate with a reference (outlet) to the navigation controller in the .xib |
| //The BIDSecondViewController class is acting as the dataSource for the table view we need to add some code | |
| //to store the names of the books written by the respective authors (in this case). | |
| @interface BIDSecondViewController : UITableViewController | |
| //The books array | |
| @property (nonatomic, strong) NSArray *books; | |
| @end |
| #import "secondViewController.h" | |
| @implementation BIDSecondViewController | |
| - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView | |
| { | |
| // Return the number of sections. | |
| return 1; | |
| } | |
| - (NSInteger)tableView:(UITableView *)tableView | |
| numberOfRowsInSection:(NSInteger)section | |
| { | |
| // Return the number of rows in the section. | |
| return [books count]; | |
| } | |
| //If the second view's data were irrelevant than the first we would initializ them in the viewDidLoad method | |
| //which is loaded once the view is initialized and that's it | |
| //Here the data are relevant to what the user will select | |
| //This gets called EACH time, and just before, the view is made visible to the user | |
| - (void)viewWillAppear:(BOOL)animated | |
| { | |
| //Animation | |
| [super viewWillAppear:animated]; | |
| //Use the title of the navigation bar to see what the user selected | |
| if ([self.title isEqualToString:@"Lehane"]) | |
| { | |
| books = [[NSArray alloc ] initWithObjects:@"Mystic River", @"Shutter Island", nil]; | |
| } | |
| else | |
| { | |
| books = [[NSArray alloc ] initWithObjects:@"The Hunt for Red October", @"Red Storm Rising", nil]; | |
| } | |
| //Call to the reloadData method of the table view | |
| //Make sure that the new data is displayed (omitting this call will result in old data appearing). | |
| [self.tableView reloadData]; | |
| } | |
| //Free memory when view unloads | |
| - (void)viewDidUnload | |
| { | |
| // Release any retained subviews of the main view. | |
| self.books = nil; | |
| } | |
| /*********************************************************/ | |
| //Draw the tables | |
| - (UITableViewCell *)tableView:(UITableView *)tableView | |
| cellForRowAtIndexPath:(NSIndexPath *)indexPath | |
| { | |
| static NSString *CellIdentifier = @"Cell"; | |
| UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; | |
| if (cell == nil) | |
| { | |
| cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; | |
| } | |
| // Configure the cell… | |
| cell.textLabel.text = [self.books objectAtIndex[indexPath row]]; | |
| return cell; | |
| } | |
| @end |
pushViewController:animated:
Makes the specified view controller the top most item on the navigation controller stack, presenting the corresponding view to the user.
popViewControllerAnimated
Removes the top most view controller from the navigation controller stack making the next view controller the currently active controller. This method is called automatically by the navigation bar “back” button provided by the navigation controller. Note when calling this method that it is not possible to pop the root view controller from the navigation controller stack.
setViewControllers:Animated
May be used to return the navigation controller stack to the state it was in the last time the application was executed. Note that this will require state information to be saved by the application on exit.
popToRootViewControllerAnimated
Pops all view controllers except the root view controller from the navigation controller stack.
popToViewController:animated
Pops all view controllers off the stack until the specified view controller is reached.
setViewControllers:animated – Pops all current view controllers (except the root view controller) from the stack and replaces them with the view controllers contained in the specified array.
HomeViewController *homeCtrl = [[HomeViewController alloc] init];
UINavigationController *homeNavCtrl = [[[UINavigationController alloc] initWithRootViewController:homeCtrl] autorelease];
[homeNavCtrl setNavigationBarHidden:YES animated:NO];
[[UINavigationBar appearance] setTitleTextAttributes:@{UITextAttributeFont : kNavigationBarTitleFont}];
[[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:@"navbar.png"] forBarMetrics:UIBarMetricsDefault];
Standard edit button
self.navigationItem.rightBarButtonItem = self.editButtonItem;
listMapBtn = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"list.png"] landscapeImagePhone:[UIImage imageNamed:@"list.png"] style:UIBarButtonItemStyleBordered target:self action:@selector(listBtnPressed:)];
self.navigationItem.rightBarButtonItem = listMapBtn;
self.editButtonItem.style = UIBarButtonItemStyleBordered;
self.editButtonItem.title = @"Custom";
self.navigationItem.rightBarButtonItem = self.editButtonItem;
self.navigationItem.rightBarButtonItem.image = [UIImage imageNamed:@"list.png"];
To remove added button, set the right/leftBarButtonItem to nil
Tutorial http://www.techotopia.com/index.php/Creating_a_Navigation_based_iOS_5_iPhone_Application_using_TableViews