Split View是iPad专用的容器,用于并排管理两个视图控制器的视图控制器。
Split View - 重要属性
- delegate
- viewControllers
Split View - 示例代码
步骤1 - 创建一个新项目,然后选择 Master Detail Application (而不是基于View的应用程序),然后单击"Next",输入项目名称并选择create。
步骤2 - 默认情况下,将创建一个简单的拆分视图控制器,该控件在master中具有表视图。
步骤3 - 这些文件创建的内容与基于View的应用程序略有不同。在这里,无涯教程为无涯教程创建了以下文件。
- AppDelegate.h
- AppDelegate.m
- DetailViewController.h
- DetailViewController.m
- DetailViewController.xib
- MasterViewController.h
- MasterViewController.m
- MasterViewController.xib
步骤4 - AppDelegate.h 文件如下-
#import <UIKit/UIKit.h>@interface AppDelegate : UIResponder <UIApplicationDelegate>
@property (strong, nonatomic) UIWindow *window;
@property (strong, nonatomic) UISplitViewController *splitViewController;
@end
步骤5 - AppDelegate.m 中的 didFinishLaunchingWithOptions 方法如下-
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen]bounds]];//应用程序启动后自定义的覆盖点。 MasterViewController masterViewController = [[MasterViewController alloc] initWithNibName:@"MasterViewController" bundle:nil]; UINavigationController masterNavigationController = [[UINavigationController alloc] initWithRootViewController: masterViewController];
DetailViewController detailViewController = [[DetailViewController alloc] initWithNibName:@"DetailViewController" bundle:nil]; UINavigationController detailNavigationController = [[UINavigationController alloc] initWithRootViewController: detailViewController];
masterViewController.detailViewController = detailViewController;
self.splitViewController = [[UISplitViewController alloc] init]; self.splitViewController.delegate = detailViewController; self.splitViewController.viewControllers = @[masterNavigationController, detailNavigationController];
self.window.rootViewController = self.splitViewController; [self.window makeKeyAndVisible]; return YES;
步骤6 - MasterViewController.h如下-
#import <UIKit/UIKit.h>@class DetailViewController; @interface MasterViewController : UITableViewController @property (strong, nonatomic) DetailViewController *detailViewController;
@end
步骤7 - MasterViewController.m如下-
#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 { [super viewDidLoad]; self.navigationItem.leftBarButtonItem = self.editButtonItem;
UIBarButtonItem *addButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem: UIBarButtonSystemItemAdd target:self action:@selector(insertNewObject:)]; self.navigationItem.rightBarButtonItem = addButton; }
- (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; //处置任何可以重新创建的资源。 }
- (void)insertNewObject:(id)sender { if (!_objects) { _objects = [[NSMutableArray alloc] init]; }
[_objects insertObject:[NSDate date] atIndex:0]; NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0]; [self.tableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation: UITableViewRowAnimationAutomatic]; }
#pragma mark - Table View
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return 1; }
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection: (NSInteger)section { return _objects.count; }
//自定义表格视图单元格的外观。 - (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]; } NSDate *object = _objects[indexPath.row]; cell.textLabel.text = [object description]; return cell; }
- (BOOL)tableView:(UITableView )tableView canEditRowAtIndexPath: (NSIndexPath )indexPath { //如果您不希望指定的项目可编辑,则返回 NO。 return YES; }
- (void)tableView:(UITableView )tableView commitEditingStyle: (UITableViewCellEditingStyle)editingStyle forRowAtIndexPath: (NSIndexPath )indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) { [_objects removeObjectAtIndex:indexPath.row]; [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation: UITableViewRowAnimationFade]; } else if (editingStyle == UITableViewCellEditingStyleInsert) { //创建相应类的新实例,将其插入 //数组,并在表格视图中添加一个新行。 } }
/* //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 { //如果您不希望该商品可重新订购,请返回 NO。 return YES; } */
- (void)tableView:(UITableView )tableView didSelectRowAtIndexPath: (NSIndexPath )indexPath { NSDate object = _objects[indexPath.row]; self.detailViewController.detailItem = object; NSDateFormatter formatter = [[NSDateFormatter alloc] init]; [formatter setDateFormat: @"yyyy-MM-dd HH:mm:ss zzz"];
NSString *stringFromDate = [formatter stringFromDate:object]; self.detailViewController.detail描述Label.text = stringFromDate; } @end
步骤8 - DetailViewController.h如下-
#import <UIKit/UIKit.h>@interface DetailViewController : UIViewController <UISplitViewControllerDelegate>
@property (strong, nonatomic) id detailItem;
@property (weak, nonatomic) IBOutlet UILabel *detail描述Label; @end
步骤9 - DetailViewController.m如下-
#import "DetailViewController.h"
@interface DetailViewController ()
@property (strong, nonatomic) UIPopoverController *masterPopoverController;
- (void)configureView;
@end
@implementation DetailViewController
#pragma mark - Managing the detail item
- (void)setDetailItem:(id)newDetailItem {
if (_detailItem != newDetailItem) {
_detailItem = newDetailItem;
</span><span class="com">//Update the view.</span><span class="pln">
</span><span class="pun">[</span><span class="kwd">self</span><span class="pln"> configureView</span><span class="pun">];</span><span class="pln">
}
if (self.masterPopoverController != nil) {
[self.masterPopoverController dismissPopoverAnimated:YES];
}
}
- (void)configureView {
//更新详细信息项的用户界面。
if (self.detailItem) {
self.detailRemarkLabel.text = [self.detailItem description];
}
}
- (void)viewDidLoad {
[super viewDidLoad];
[self configureView];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
//处置任何可以重新创建的资源。
}
- (id)initWithNibName:(NSString )nibNameOrNil bundle:
(NSBundle )nibBundleOrNil {
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
self.title = NSLocalizedString(@"Detail", @"Detail");
}
return self;
}
#pragma mark - Split view
- (void)splitViewController:(UISplitViewController )splitController
willHideViewController:(UIViewController )viewController withBarButtonItem:
(UIBarButtonItem )barButtonItem forPopoverController:
(UIPopoverController )popoverController {
barButtonItem.title = NSLocalizedString(@"Master", @"Master");
[self.navigationItem setLeftBarButtonItem:barButtonItem animated:YES];
self.masterPopoverController = popoverController;
}
- (void)splitViewController:(UISplitViewController )splitController
willShowViewController:(UIViewController )viewController
invalidatingBarButtonItem:(UIBarButtonItem *)barButtonItem {
//当视图在拆分视图中再次显示时调用,
//使按钮和弹出框控制器无效。
[self.navigationItem setLeftBarButtonItem:nil animated:YES];
self.masterPopoverController = nil;
}
@end
步骤10 - 运行应用程序时,无涯教程将在横向模式下获得以下输出-

步骤11 - 切换模式时,无涯教程将获得以下输出-