Interacting Between Master and Detail View using Delegates in Objective-C
suggest changeUISplitViewController needs to be the root view controller of your app’s window. The split-view class in this sample is ViewController. AppDelegate originally allocated SplitViewController, which is not that class. initWithFrame: was missing its semicolon. The (id) casts hide missing protocol declarations. Cell identifiers include the index path, so cells are not reused. Detail inserts another controller’s view without addChildViewController:, so that child does not receive the normal appearance callbacks.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.window.backgroundColor = [UIColor blackColor];
[self.window makeKeyAndVisible];
self.window.clipsToBounds = YES;
ViewController *spView = [[ViewController alloc] init];
self.window.rootViewController = spView;
[self.window makeKeyAndVisible];
return YES;
}
Create a UISplitViewController and set it as the window’s root view controller.
#import <UIKit/UIKit.h>
#import "MasterViewController.h"
#import "DetailViewController.h"
@interface ViewController : UISplitViewController
{
DetailViewController *detailVC;
MasterViewController *masterVC;
NSMutableArray *array;
}
@end
MasterViewController is a UIViewController on the leading side. Set its width with maximumPrimaryColumnWidth. DetailViewController is on the trailing side. DetailViewController implements sendSelectedNavController: but its header does not adopt DetailViewDelegate.
#import "ViewController.h"
#define ANIMATION_LENGTH 0.3
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
masterVC = [[MasterViewController alloc]init];
detailVC = [[DetailViewController alloc]init];
[masterVC setDetailDelegate:(id)detailVC];
NSArray *vcArray = [NSArray arrayWithObjects:masterVC, detailVC, nil];
self.preferredDisplayMode = UISplitViewControllerDisplayModeAutomatic;
self.viewControllers = vcArray;
self.delegate = (id)self;
self.presentsWithGesture = YES;
}
@end
The master and detail view controllers are added to an NSArray which is set to self.viewControllers. preferredDisplayMode is how the master and detail are shown. presentsWithGesture enables the swipe that shows the master.
#import <UIKit/UIKit.h>
@protocol DetailViewDelegate <NSObject>
@required
- (void)sendSelectedNavController:(UIViewController *)viewController;
@end
@interface MasterViewController : UIViewController
{
UITableView *mainTableView;
NSMutableArray *viewControllerArray;
}
@property (nonatomic, retain) id<DetailViewDelegate> detailDelegate;
@end
Create a DetailViewDelegate protocol with sendSelectedNavController: for sending a view controller to the detail side. MasterViewController then creates a table view. viewControllerArray contains the view controllers to show in the detail controller.
#import "MasterViewController.h"
@implementation MasterViewController
@synthesize detailDelegate;
-(void)viewDidLoad
{
[super viewDidLoad];
UIViewController *dashBoardVC = [[UIViewController alloc]init];
[dashBoardVC.view setBackgroundColor:[UIColor redColor]];
UIViewController *inventVC = [[UIViewController alloc]init];
[inventVC.view setBackgroundColor:[UIColor whiteColor]];
UIViewController *alarmVC = [[UIViewController alloc]init];
[alarmVC.view setBackgroundColor: [UIColor purpleColor]];
UIViewController *scanDeviceVC = [[UIViewController alloc]init];
[scanDeviceVC.view setBackgroundColor:[UIColor cyanColor]];
UIViewController *serverDetailVC = [[UIViewController alloc]init];
[serverDetailVC.view setBackgroundColor: [UIColor whiteColor]];
viewControllerArray = [[NSMutableArray alloc]initWithObjects:dashBoardVC,inventVC,alarmVC,scanDeviceVC,serverDetailVC,nil];
mainTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 50,self.view.frame.size.width, self.view.frame.size.height-50) style:UITableViewStylePlain];
[mainTableView setDelegate:(id)self];
[mainTableView setDataSource:(id)self];
[mainTableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];
[mainTableView setScrollsToTop:NO];
[self.view addSubview:mainTableView];
}
- (CGFloat)tableView:(UITableView *)tableView
heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 100;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection: (NSInteger)section
{
return [viewControllerArray count];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1; //count of section
}
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *cellId = [NSString stringWithFormat:@"Cell%li%ld",(long)indexPath.section,(long)indexPath.row];
UITableViewCell *cell =[tableView dequeueReusableCellWithIdentifier:cellId];
if (cell == nil)
{
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId];
}
[cell.contentView setBackgroundColor:[UIColor redColor]];
cell.textLabel.text =[NSString stringWithFormat:@"My VC at index %ld",(long)indexPath.row];
return cell;
}
- (void)tableView:(UITableView *)tableView
didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[detailDelegate sendSelectedNavController:[viewControllerArray objectAtIndex:indexPath.row]];
}
@end
The master creates view controllers, stores them in an NSMutableArray, and on didSelectRowAtIndexPath: sends the selected one through detailDelegate.
#import <UIKit/UIKit.h>
@interface DetailViewController : UIViewController
{
UIViewController *tempNav;
}
@end
#import "DetailViewController.h"
@implementation DetailViewController
-(void)viewDidLoad
{
[super viewDidLoad];
[self.view setBackgroundColor:[UIColor whiteColor]];
}
-(void)sendSelectedNavController:(UIViewController *)navController
{
NSArray *viewsToRemove = [self.view subviews];
for (UIView *v in viewsToRemove) {
[v removeFromSuperview];
}
tempNav = navController;
[self.view addSubview:tempNav.view];
}
@end
The sendSelectedNavController is declared here with removing all the UIView’s in the DetailViewController and adding the passed UIViewController from the MasterViewController.