{"id":570,"date":"2018-02-23T23:40:06","date_gmt":"2018-02-23T15:40:06","guid":{"rendered":"http:\/\/60.205.208.74\/2018\/02\/23\/ios%e5%88%86%e5%89%b2%e8%a7%86%e5%9b%besplit-view\/"},"modified":"2018-02-23T23:40:06","modified_gmt":"2018-02-23T15:40:06","slug":"ios%e5%88%86%e5%89%b2%e8%a7%86%e5%9b%besplit-view","status":"publish","type":"post","link":"https:\/\/www.puzi7.net\/?p=570","title":{"rendered":"iOS\u5206\u5272\u89c6\u56fe(Split View)"},"content":{"rendered":"<span itemprop=\"description\"><div class=\"article-body\">\n<div class=\"article-intro\" id=\"content\">\n<h2>\u5206\u5272\u89c6\u56fe\u7684\u4f7f\u7528<\/h2>\n<p>\u5206\u5272\u89c6\u56fe\u662f iPad \u7684\u7279\u5b9a\u89c6\u56fe\u63a7\u5236\u5668\u7528\u4e8e\u7ba1\u7406\u4e24\u4e2a\u89c6\u56fe\u63a7\u5236\u5668\uff0c\u5728\u5de6\u4fa7\u662f\u4e00\u4e2a\u4e3b\u63a7\u5236\u5668\uff0c\u5176\u53f3\u4fa7\u662f\u4e00\u4e2a\u8be6\u7ec6\u4fe1\u606f\u89c6\u56fe\u63a7\u5236\u5668\u3002<br \/>\n\u91cd\u8981\u7684\u5c5e\u6027<\/p>\n<ul>\n<li>delegate<\/li>\n<li>viewControllers<\/li>\n<\/ul>\n<h2>\u793a\u4f8b\u4ee3\u7801\u548c\u6b65\u9aa4<\/h2>\n<p>1.\u521b\u5efa\u4e00\u4e2a\u65b0\u9879\u76ee\uff0c\u9009\u62e9Master Detail Application\u5e76\u5355\u51fb\u4e0b\u4e00\u6b65\uff0c\u8f93\u5165\u9879\u76ee\u540d\u79f0\uff0c\u7136\u540e\u9009\u62e9\u521b\u5efa\u3002<\/p>\n<p>2.\u7b80\u5355\u7684\u5206\u5272\u89c6\u56fe\u63a7\u5236\u5668\u4e0e\u6bcd\u7248\u4e2d\u7684\u8868\u89c6\u56fe\u662f\u9ed8\u8ba4\u521b\u5efa\u7684\u3002<\/p>\n<p>3.\u5728\u8fd9\u91cc\u6211\u4eec\u4e3a\u6211\u4eec\u521b\u5efa\u7684\u4e0b\u5217\u6587\u4ef6\u3002<\/p>\n<ul>\n<li>AppDelegate.h<\/li>\n<li>AppDelegate.m<\/li>\n<li>DetailViewController.h<\/li>\n<li>DetailViewController.m<\/li>\n<li>DetailViewController.xib<\/li>\n<li>MasterViewController.h<\/li>\n<li>MasterViewController.m<\/li>\n<li>MasterViewController.xib<\/li>\n<\/ul>\n<p>4. AppDelegate.h\u6587\u4ef6\u5982\u4e0b\u6240\u793a<\/p>\n<pre>\n#import &lt;UIKit\/UIKit.h&gt;\n\n@interface AppDelegate : UIResponder &lt;UIApplicationDelegate&gt;\n\n@property (strong, nonatomic) UIWindow *window;\n\n@property (strong, nonatomic) UISplitViewController *splitViewController;\n\n@end\n<\/pre>\n<p>5.\u5728AppDelegate.m\u4e2d\u7684didFinishLaunchingWithOptions\u65b9\u6cd5\uff0c\u5982\u4e0b\u6240\u793a<\/p>\n<pre>\n - (BOOL)application:(UIApplication *)application \n   didFinishLaunchingWithOptions:(NSDictionary *)launchOptions\n   self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen]\n   bounds]];\n    \/\/ Override point for customization after application launch.\n    MasterViewController *masterViewController = [[MasterViewController\n    alloc] initWithNibName:@\"MasterViewController\" bundle:nil];\n    UINavigationController *masterNavigationController = \n    [[UINavigationController alloc] initWithRootViewController:\n    masterViewController];\n\n    DetailViewController *detailViewController = \n    [[DetailViewController alloc] initWithNibName:@\"DetailViewController\" \n    bundle:nil];\n    UINavigationController *detailNavigationController = \n    [[UINavigationController alloc] initWithRootViewController:\n    detailViewController];\n\n    masterViewController.detailViewController = detailViewController;\n\n    self.splitViewController = [[UISplitViewController alloc] init];\n    self.splitViewController.delegate = detailViewController;\n    self.splitViewController.viewControllers =\n    @[masterNavigationController, detailNavigationController];\n    self.window.rootViewController = self.splitViewController;\n    [self.window makeKeyAndVisible];\n    return YES;\n}\n<\/pre>\n<p>6. MasterViewController.h\uff0c\u5982\u4e0b\u6240\u793a<\/p>\n<pre>\n#import &lt;UIKit\/UIKit.h&gt;\n\n@class DetailViewController;\n\n@interface MasterViewController : UITableViewController\n\n@property (strong, nonatomic) DetailViewController *detailViewController;\n\n@end\n<\/pre>\n<p>7. MasterViewController.m\uff0c\u5982\u4e0b\u6240\u793a<\/p>\n<pre>\n#import \"MasterViewController.h\"\n\n#import \"DetailViewController.h\"\n\n@interface MasterViewController () {\n    NSMutableArray *_objects;\n}\n@end\n\n@implementation MasterViewController\n\n- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)\n  nibBundleOrNil\n{\n    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];\n    if (self) {\n        self.title = NSLocalizedString(@\"Master\", @\"Master\");\n        self.clearsSelectionOnViewWillAppear = NO;\n        self.contentSizeForViewInPopover = CGSizeMake(320.0, 600.0);\n    }\n    return self;\n}\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\n- (void)viewDidLoad\n{\n    [super viewDidLoad];\n    self.navigationItem.leftBarButtonItem = self.editButtonItem;\n\n    UIBarButtonItem *addButton = [[UIBarButtonItem alloc] \n    initWithBarButtonSystemItem: UIBarButtonSystemItemAdd \n    target:self action:@selector(insertNewObject:)];\n    self.navigationItem.rightBarButtonItem = addButton;\n}\n\n- (void)didReceiveMemoryWarning\n{\n    [super didReceiveMemoryWarning];\n    \/\/ Dispose of any resources that can be recreated.\n}\n\n- (void)insertNewObject:(id)sender\n{\n    if (!_objects) {\n        _objects = [[NSMutableArray alloc] init];\n    }\n    [_objects insertObject:[NSDate date] atIndex:0];\n    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];\n    [self.tableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:\n    UITableViewRowAnimationAutomatic];\n}\n\n#pragma mark - Table View\n\n- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView\n{\n    return 1;\n}\n\n- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:\n  (NSInteger)section\n{\n    return _objects.count;\n}\n\n\/\/ Customize the appearance of table view cells.\n- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:\n  (NSIndexPath *)indexPath\n{\n    static NSString *CellIdentifier = @\"Cell\";\n    \n    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:\n    CellIdentifier];\n    if (cell == nil) {\n        cell = [[UITableViewCell alloc] initWithStyle:\n        UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];\n    }\n\n\n    NSDate *object = _objects[indexPath.row];\n    cell.textLabel.text = [object description];\n    return cell;\n}\n\n- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:\n  (NSIndexPath *)indexPath\n{\n    \/\/ Return NO if you do not want the specified item to be editable.\n    return YES;\n}\n\n- (void)tableView:(UITableView *)tableView commitEditingStyle:\n  (UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:\n  (NSIndexPath *)indexPath\n{\n    if (editingStyle == UITableViewCellEditingStyleDelete) {\n        [_objects removeObjectAtIndex:indexPath.row];\n        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:\n        UITableViewRowAnimationFade];\n    } else if (editingStyle == UITableViewCellEditingStyleInsert) {\n        \/\/ Create a new instance of the appropriate class, insert it into \n        \/\/the array, and add a new row to the table view.\n    }\n}\n\n\/*\n\/\/ Override to support rearranging the table view.\n- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:\n  (NSIndexPath *) fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath\n{\n}\n*\/\n\n\/*\n\/\/ Override to support conditional rearranging of the table view.\n- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:\n  (NSIndexPath *)indexPath\n{\n    \/\/ Return NO if you do not want the item to be re-orderable.\n    return YES;\n}\n*\/\n\n- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:\n  (NSIndexPath *)indexPath\n{\n    NSDate *object = _objects[indexPath.row];\n    self.detailViewController.detailItem = object;\n    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];\n    [formatter setDateFormat: @\"yyyy-MM-dd HH:mm:ss zzz\"];  \n    NSString *stringFromDate = [formatter stringFromDate:object];\n    self.detailViewController.detailDescriptionLabel.text = stringFromDate;\n}\n\n@end\n\n<\/pre>\n<p>8. DetailViewController.h \uff0c\u5982\u4e0b\u6240\u793a<\/p>\n<pre>\n#import &lt;UIKit\/UIKit.h&gt;\n\n@interface DetailViewController : UIViewController\n&lt;UISplitViewControllerDelegate&gt;\n\n@property (strong, nonatomic) id detailItem;\n\n@property (weak, nonatomic) IBOutlet UILabel *detailDescriptionLabel;\n@end\n<\/pre>\n<p>9. DetailViewController.m \uff0c\u5982\u4e0b\u6240\u793a<\/p>\n<pre>\n#import \"DetailViewController.h\"\n\n@interface DetailViewController ()\n@property (strong, nonatomic) UIPopoverController *masterPopoverController;\n- (void)configureView;\n@end\n\n@implementation DetailViewController\n\n#pragma mark - Managing the detail item\n\n- (void)setDetailItem:(id)newDetailItem\n{\n    if (_detailItem != newDetailItem) {\n        _detailItem = newDetailItem;\n        \n        \/\/ Update the view.\n        [self configureView];\n    }\n\n    if (self.masterPopoverController != nil) {\n        [self.masterPopoverController dismissPopoverAnimated:YES];\n    }        \n}\n\n- (void)configureView\n{\n    \/\/ Update the user interface for the detail item.\n\n    if (self.detailItem) {\n        self.detailDescriptionLabel.text = [self.detailItem description];\n    }\n}\n\n- (void)viewDidLoad\n{\n    [super viewDidLoad];\n    [self configureView];\n}\n\n- (void)didReceiveMemoryWarning\n{\n    [super didReceiveMemoryWarning];\n    \/\/ Dispose of any resources that can be recreated.\n}\n\n- (id)initWithNibName:(NSString *)nibNameOrNil bundle:\n  (NSBundle *)nibBundleOrNil\n{\n    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];\n    if (self) {\n        self.title = NSLocalizedString(@\"Detail\", @\"Detail\");\n    }\n    return self;\n}\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\n#pragma mark - Split view\n\n- (void)splitViewController:(UISplitViewController *)splitController \n  willHideViewController:(UIViewController *)viewController withBarButtonItem:\n  (UIBarButtonItem *)barButtonItem forPopoverController:\n  (UIPopoverController *)popoverController\n{\n    barButtonItem.title = NSLocalizedString(@\"Master\", @\"Master\");\n    [self.navigationItem setLeftBarButtonItem:barButtonItem animated:YES];\n    self.masterPopoverController = popoverController;\n}\n\n- (void)splitViewController:(UISplitViewController *)splitController \n  willShowViewController:(UIViewController *)viewController \n  invalidatingBarButtonItem:(UIBarButtonItem *)barButtonItem\n{\n    \/\/ Called when the view is shown again in the split view, \n    \/\/invalidating the button and popover controller.\n    [self.navigationItem setLeftBarButtonItem:nil animated:YES];\n    self.masterPopoverController = nil;\n}\n\n@end\n<\/pre>\n<p>10.\u73b0\u5728\u5f53\u6211\u4eec\u8fd0\u884c\u5e94\u7528\u7a0b\u5e8f\u65f6\uff0c\u5728\u6a2a\u5411\u6a21\u5f0f\u4e0b\u6211\u4eec\u4f1a\u5f97\u5230\u4e0b\u9762\u7684\u8f93\u51fa<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"attachment-full\" alt=\"masterDetailOutput1\" src=\"http:\/\/www.runoob.com\/wp-content\/uploads\/2013\/11\/masterDetailOutput1.jpg\" width=\"560\" height=\"437\" data-pinit=\"registered\"><\/p>\n<p>11. \u5f53\u6211\u4eec\u5207\u6362\u5230\u7eb5\u5411\u6a21\u5f0f\uff0c\u6211\u4eec\u4f1a\u83b7\u5f97\u4e0b\u9762\u7684\u8f93\u51fa:\n<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"attachment-full\" alt=\"masterDetailOutput2\" src=\"http:\/\/www.runoob.com\/wp-content\/uploads\/2013\/11\/masterDetailOutput2.jpg\" width=\"500\" height=\"689\" data-pinit=\"registered\"><\/p>\n<\/p><\/div>\n<\/p><\/div>\n<span>","protected":false},"excerpt":{"rendered":"<p class=\"excerpt\"><span itemprop=\"description\">\u5206\u5272\u89c6\u56fe\u7684\u4f7f\u7528 \u5206\u5272\u89c6\u56fe\u662f iPad \u7684\u7279\u5b9a\u89c6\u56fe\u63a7\u5236\u5668\u7528\u4e8e\u7ba1\u7406\u4e24\u4e2a\u89c6\u56fe\u63a7\u5236\u5668\uff0c\u5728\u5de6\u4fa7\u662f\u4e00\u4e2a&hellip;<\/span><\/p>\n<p class=\"more-link-p btn-align-right\"><a class=\"green zoom-btn\" href=\"https:\/\/www.puzi7.net\/?p=570\">Read More<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[38,26],"tags":[74,93],"class_list":["post-570","post","type-post","status-publish","format-standard","hentry","category-ios","category-mobile","tag-ios","tag-mobile","zoom-theme-has-thumb"],"_links":{"self":[{"href":"https:\/\/www.puzi7.net\/index.php?rest_route=\/wp\/v2\/posts\/570","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.puzi7.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.puzi7.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.puzi7.net\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.puzi7.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=570"}],"version-history":[{"count":0,"href":"https:\/\/www.puzi7.net\/index.php?rest_route=\/wp\/v2\/posts\/570\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.puzi7.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=570"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.puzi7.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=570"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.puzi7.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=570"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}