Skip to content

Instantly share code, notes, and snippets.

@lamprosg
Last active December 14, 2015 03:59
Show Gist options
  • Select an option

  • Save lamprosg/5024778 to your computer and use it in GitHub Desktop.

Select an option

Save lamprosg/5024778 to your computer and use it in GitHub Desktop.
(iOS) Navigation based application - using TableViews
//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
- (void)viewDidLoad
{
[super viewDidLoad];
UIBarButtonItem *anotherButton = [[UIBarButtonItem alloc] initWithTitle:@"Show" style:UIBarButtonItemStylePlain target:self action:@selector(yourFunction)];
self.navigationItem.rightBarButtonItem = anotherButton;
//OR MULTIPLE BUTTONS
UIBarButtonItem *emailBtn = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"emailBtn.png"] style:UIButtonTypeCustom target:self action:@selector(emailBtnClicked)];
UIBarButtonItem *phoneBtn = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"phone.png"] style:UIButtonTypeCustom target:self action:@selector(phoneBtnClicked)];
self.navigationItem.rightBarButtonItems = [NSArray arrayWithObjects:phoneBtn, emailBtn, nil];
[phoneBtn release];
[emailBtn release];
[anotherButton release];
}
//You can also add a left button
----------------------------------------------------
//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
@lamprosg
Copy link
Copy Markdown
Author

lamprosg commented Mar 1, 2013

[[UINavigationBar appearance] setTitleTextAttributes:@{UITextAttributeFont : kNavigationBarTitleFont}];
[[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:@"navbar.png"] forBarMetrics:UIBarMetricsDefault];

@lamprosg
Copy link
Copy Markdown
Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment