Skip to content

Commit

Permalink
Added some example code for handling the didSelectRowAtIndexPath even…
Browse files Browse the repository at this point in the history
…t for

a master view within a split view. The code shows how to change the corresponding
detail view.
  • Loading branch information
Andreas Katzian committed Oct 4, 2010
1 parent dfccab2 commit 700797c
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 1 deletion.
49 changes: 48 additions & 1 deletion MasterDetail/Classes/MasterViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,54 @@ - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(N
#pragma mark Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
//do nothing at the moment

//need to remove cell selection
[tableView deselectRowAtIndexPath:indexPath animated:NO];

//if there is now split view controller this controller
//is not used as a master controller
if(self.splitViewController == nil)
return;

//since we are the master view controller of a split view
//we know there is a parent UISplitViewController
NSArray *controllers = self.splitViewController.viewControllers;

//Excerpt from the documentation:
//http://developer.apple.com/library/ios/#documentation/UIKit/Reference/UISplitViewController_class/Reference/Reference.html
//The array in this property must contain exactly two view controllers.
//The view controllers are presented left-to-right in the split view interface
//when it is in a landscape orientation. Thus, the view controller at index 0
//is displayed on the left side and the view controller at index 1 is displayed
//on the right side of the interface.

//if there aren't at least two controllers somethin went wrong
if([controllers count] < 2)
return;

//get the detail controller
UIViewController *detailViewController = [controllers objectAtIndex:1];

//now you can do whatever you want to change
//controls or text on detail view controller
//EXAMPLE:
//UIView *detailView = detailViewController.view;
//detailView.backgroundColor = (indexPath.row % 2 == 0) ? [UIColor grayColor] : [UIColor blackColor];

//if you want to change the detail view controller
//just create a new array containing the master and
//new detail controller and set the property "viewControllers"
//of the current split view controller
UIViewController *newDetailController = [[UIViewController alloc] init];
UIView *newDetailView = [[UIView alloc] initWithFrame:detailViewController.view.frame];
newDetailView.backgroundColor = [UIColor yellowColor];
newDetailController.view = newDetailView;

NSArray *newControllers = [NSArray arrayWithObjects:self, newDetailController, nil];
self.splitViewController.viewControllers = newControllers;

[newDetailController release];
[newDetailView release];
}


Expand Down
7 changes: 7 additions & 0 deletions MasterDetail/MasterDetail.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,14 @@
isa = PBXProject;
buildConfigurationList = C01FCF4E08A954540054247B /* Build configuration list for PBXProject "MasterDetail" */;
compatibilityVersion = "Xcode 3.1";
developmentRegion = English;
hasScannedForEncodings = 1;
knownRegions = (
English,
Japanese,
French,
German,
);
mainGroup = 29B97314FDCFA39411CA2CEA /* CustomTemplate */;
projectDirPath = "";
projectRoot = "";
Expand Down

0 comments on commit 700797c

Please sign in to comment.