Usage of Split View
A Split View is an iPad-specific view controller used to manage two view controllers. On the left is a master controller, and on the right is a detail view controller. Important properties:
- delegate
- viewControllers
Sample Code and Steps
- Create a new project, select Master Detail Application and click Next. Enter the project name, then choose Create.
- A simple Split View Controller with a table view in the master is created by default.
- Here are the files created for us:
- AppDelegate.h
- AppDelegate.m
- DetailViewController.h
- DetailViewController.m
- DetailViewController.xib
- MasterViewController.h
- MasterViewController.m
- MasterViewController.xib
4. The AppDelegate.h file is as follows:
#import <UIKit/UIKit.h>@interface AppDelegate : UIResponder <UIApplicationDelegate>@property (strong, nonatomic) UIWindow *window;@property (strong, nonatomic) UISplitViewController *splitViewController;@end
5. The didFinishLaunchingWithOptions method in AppDelegate.m is as follows:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions self.window = [ initWithFrame:[ bounds]]; // Override point for customization after application launch. MasterViewController *masterViewController = [ initWithNibName:@"MasterViewController" bundle:nil]; UINavigationController *masterNavigationController = [ initWithRootViewController: masterViewController]; DetailViewController *detailViewController = [ initWithNibName:@"DetailViewController" bundle:nil]; UINavigationController *detailNavigationController = [ initWithRootViewController: detailViewController]; masterViewController.detailViewController = detailViewController; self.splitViewController = [ init]; self.splitViewController.delegate = detailViewController; self.splitViewController.viewControllers = @[masterNavigationController, detailNavigationController]; self.window.rootViewController = self.splitViewController; [self.window makeKeyAndVisible]; return YES;}
6. MasterViewController.h is as follows:
#import <UIKit/UIKit.h>@class DetailViewController;@interface MasterViewController : UITableViewController@property (strong, nonatomic) DetailViewController *detailViewController;@end
7. MasterViewController.m is as follows:
#import "MasterViewController.h"#import "DetailViewController.h"@interface MasterViewController () { NSMutableArray *_objects;}@end@implementation MasterViewController- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *) nibBundleOrNil { self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; if (self) { self.title = NSLocalizedString(@"Master", @"Master"); self.clearsSelectionOnViewWillAppear = NO; self.contentSizeForViewInPopover = CGSizeMake(320.0, 600.0); } return self;}- (void)viewDidLoad { ; self.navigationItem.leftBarButtonItem = self.editButtonItem; UIBarButtonItem *addButton = [ initWithBarButtonSystemItem: UIBarButtonSystemItemAdd target:self action:@selector(insertNewObject:)]; self.navigationItem.rightBarButtonItem = addButton;}- (void)didReceiveMemoryWarning { ; // Dispose of any resources that can be recreated.}- (void)insertNewObject:(id)sender { if (!_objects) { _objects = [ init]; } [_objects insertObject: atIndex:0]; NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0]; [self.tableView insertRowsAtIndexPaths:@ withRowAnimation: UITableViewRowAnimationAutomatic];}#pragma mark - Table View- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return 1;}- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection: (NSInteger)section { return _objects.count;}// Customize the appearance of table view cells.- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath: (NSIndexPath *)indexPath { static NSString *CellIdentifier = @"Cell"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier: CellIdentifier]; if (cell == nil) { cell = [ initWithStyle: UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; } NSDate *object = _objects[indexPath.row]; cell.textLabel.text = ; return cell;}- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath: (NSIndexPath *)indexPath { // Return NO if you do not want the specified item to be editable. return YES;}- (void)tableView:(UITableView *)tableView commitEditingStyle: (UITableViewCellEditingStyle)editingStyle forRowAtIndexPath: (NSIndexPath *)indexPath { if (editingStyle == UITableViewCellEditingStyleDelete) { [_objects removeObjectAtIndex:indexPath.row]; [tableView deleteRowsAtIndexPaths:@ withRowAnimation: UITableViewRowAnimationFade]; } else if (editingStyle == UITableViewCellEditingStyleInsert) { // Create a new instance of the appropriate class, insert it into //the array, and add a new row to the table view. }}/* // Override to support rearranging the table view. - (void)tableView:(UITableView *)tableView moveRowAtIndexPath: (NSIndexPath *) fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath { } *//* // Override to support conditional rearranging of the table view. - (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath: (NSIndexPath *)indexPath { // Return NO if you do not want the item to be re-orderable. return YES; } */- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath: (NSIndexPath *)indexPath { NSDate *object = _objects[indexPath.row]; self.detailViewController.detailItem = object; NSDateFormatter *formatter = [ init]; [formatter setDateFormat: @"yyyy-MM-dd HH:mm:ss zzz"]; NSString *stringFromDate = [formatter stringFromDate:object]; self.detailViewController.detailDescriptionLabel.text = stringFromDate;}@end
8. DetailViewController.h is as follows:
#import <UIKit/UIKit.h>@interface DetailViewController : UIViewController<UISplitViewControllerDelegate>@property (strong, nonatomic) id d
YouTip