Preparing the AppDelegate code
-
Open AppDelegate.h in Xcode. Modify the code so it is the same
as the following:
#import <UIKit/UIKit.h> @class ViewController; @interface AppDelegate : UIResponder <UIApplicationDelegate> @property (strong, nonatomic) UIWindow *window; @property (strong, nonatomic) UIViewController *viewController; @property (strong, nonatomic) UINavigationController *navController; @end
This adds three properties to keep track of the window, the main view controller and the navigation controller.
- Now open AppDelegate.m into Xcode (you can do this by simply clicking on it in the navigation view).
-
Modify the AppDelegate.m code so that is the same as the
following:
#import "AppDelegate.h" #import "HelloRepoViewController.h" @implementation AppDelegate - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; self.viewController = [[HelloRepoViewController alloc] init]; self.navController = [[UINavigationController alloc] initWithRootViewController:self.viewController]; self.window.rootViewController = self.navController; [self.window makeKeyAndVisible]; return YES; } @end
This code creates the main window, the HelloRepo view controller and the navigation controller and stores pointers to them in the AppDelegate object. This makes it easier to refer to these objects in the HelloRepo code. The code initializes the navigation controller with the HelloRepo view controller, and makes the navigation controller the root view controller.
Adding the node detail controller
-
Note: In this step it is very important to ensure that the class created is a subclass of UITableViewController. When you create this class you must select UITableViewController from the Subclass of drop-down listbox.In Xcode, right-click the HelloRepo group (groups are identified by a folder icon) and select New File....
- Select Objective-C class and click Next.
-
Enter the class name as DocumentTableViewController and select
UITableViewController from the Subclass
of drop-down listbox. Ensure that both checkboxes are clear (you are not
targeting iPad or using XIBs). Click Next. Then click
Create.
Xcode will create two new files in your project: DocumentTableViewController.h and DocumentTableViewController.m. This class will be a subclass of UITableViewController.
-
Check the file DocumentTableViewController.h. The contents
should be as follows:
#import <UIKit/UIKit.h> @interface DocumentTableViewController : UITableViewController @end
Note: In particular, note that the new class is a subclass of UITableViewController. -
Note: When this controller is ultimately created you will need to pass it two pieces of information: the node that has just been selected in the main view, and the current session object. To do this you can create an initialization method to receive this information and set two instance variables. The selected node is passed so it is possible to determine which node to fetch details for. The session object is used by methods that return services.First set up the header file DocumentTableViewController.h. Open DocumentTableViewController.h in Xcode.
-
Modify DocumentTableViewController.h so it matches the following
code:
#import <UIKit/UIKit.h> #import "AlfrescoSession.h" #import "AlfrescoNode.h" @interface DocumentTableViewController : UITableViewController @property (strong, nonatomic) SkyVaultNode *node; @property (strong, nonatomic) id<AlfrescoSession> session; - (id)initWithSession:(id<AlfrescoSession>)createdSession andNode:(AlfrescoNode *)nodeToBeDisplayed; @end
This includes necessary header files, adds a couple of properties to store the selected node and current session, and adds a prototype for the new initialization method.
-
Now, add the initialization method itself. Open
DocumentTableViewController.m and add the following method:
- (id)initWithSession:(id<AlfrescoSession>)createdSession andNode:(AlfrescoNode *)nodeToBeDisplayed { if ((self = [super init])) { self.node = nodeToBeDisplayed; self.session = createdSession; } return self; }
-
Now clean out unnecessary code. Load
DocumentTableViewController.m in Xcode. Navigate to the method
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath
*)indexPath. You will see that this method and several methods after it are
already commented out. These are used for editable tables. As we will not be using this
feature in this tutorial, the following methods can safely be removed:
- - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
- - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle
- - (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath
- - (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
- Test your code by doing a . Everything should compile without error - although you will receive several warnings due to warning pragmas in code that was created automatically by Xcode. You will add code to fix this in later tutorials. You can run the code, but as yet the new DocumentTableViewController has not be linked to from the main table view code.
How to add disclosure indicators to table cells in the main view
- Open HelloRepoViewController.m and navigate to the tableView:cellForRowAtIndexPath: method. Identify the if statement within the method.
-
Modify the if block in the code so that it matches the
following:
if (cell == nil) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; }
The second statement is the one that adds the disclosure indicator.
Adding row selection in the table view
-
You will need to reference the AppDelegate and
DocumentTableViewController classes from your code, so their header
files need to be included into HelloRepoViewController.m. Open
HelloRepoViewController.m and at the top of the file add the
following imports:
#import "DocumentTableViewController.h" #import "AppDelegate.h"
-
Still in HelloRepoViewController.m, navigate to #pragma
mark - Rotation at the bottom of the file. Before this point in the code add
the following code:
#pragma mark - TableView delegate - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { // Obtain the node for the selected row SkyVaultNode *node = [self.nodes objectAtIndex:indexPath.row]; // Obtain a pointer to the AppDelegate instance AppDelegate *delegate =(AppDelegate *)[[UIApplication sharedApplication] delegate]; // Create and initialize the new node detail view controller DocumentTableViewController *documentTableViewController = [[DocumentTableViewController alloc] initWithSession:self.session andNode:node]; // push onto UINavigationController's stack [delegate.navController pushViewController:documentTableViewController animated:YES]; }
When a row in the table view is selected the node corresponding with that location is stored in the pointer variable node. The code then obtains the app delegate object so that its properties, in particular the navigation controller instance, can be accessed. The new node view (DocumentTableViewController) is then created - this is where details about the node will be displayed. Note the new view is initialized with details of the selected node and the current session. The session object will be used later to retrieve SkyVault services, so that node details can be retrieved and then displayed. The code then pushes the node detail view onto the navigation controller's stack. This has the effect that when a node is selected, a new view slides into position and will ultimately (in later tutorials) display node information.
In this tutorial you extended HelloRepo to include a node detail view.