iOS – Split View(分割视图)

iOS – Split View(分割视图)使用拆分视图在IOS中拆分视图是iPad特定的容器管理两个视图控制器并排,一个在左,详细视图控制器在其右侧的主视图控制器。重要的属性delegateviewControllers示例代码和步骤1.创建一个新的项目,并选择MasterDetailApplication,而不是基于视图的应用程

大家好,欢迎来到IT知识分享网。

使用拆分视图

在 IOS 中拆分视图是iPad特定的容器管理两个视图控制器并排,一个在左,详细视图控制器在其右侧的主视图控制器。

 

重要的属性

  • delegate

  • viewControllers

 

示例代码和步骤

1. 创建一个新的项目,并选择 Master Detail Application,而不是基于视图的应用程序,然后单击下一步,为项目名称,选择“创建”。

2. 默认情况下,创建一个简单的拆分视图控制器在主表视图。

3. 创建的文件从我们基于视图的应用程序可能有点不同。在这里,我们有以下的为我们创建的文件。

  • AppDelegate.h

  • AppDelegate.m

  • DetailViewController.h

  • DetailViewController.m

  • DetailViewController.xib

  • MasterViewController.h

  • MasterViewController.m

  • MasterViewController.xib

4. AppDelegate.h 内容如下:

#import <UIKit/UIKit.h>@interfaceAppDelegate:UIResponder<UIApplicationDelegate>@property(strong, nonatomic)UIWindow*window;@property(strong, nonatomic)UISplitViewController*splitViewController;@end

5.  didFinishLaunchingWithOptions 方法在 AppDelegate.m 如下:

-(BOOL)application:(UIApplication*)application 
   didFinishLaunchingWithOptions:(NSDictionary*)launchOptions
   self.window =[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen]
   bounds]];// Override point for customization after application launch.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>@classDetailViewController;@interfaceMasterViewController:UITableViewController@property(strong, nonatomic)DetailViewController*detailViewController;@end

7. MasterViewController.m 内容如下:

#import "MasterViewController.h"#import "DetailViewController.h"@interfaceMasterViewController(){NSMutableArray*_objects;}@end@implementationMasterViewController-(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);}returnself;}-(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];// Dispose of any resources that can be recreated.}-(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 -TableView-(NSInteger)numberOfSectionsInTableView:(UITableView*)tableView
{return1;}-(NSInteger)tableView:(UITableView*)tableView numberOfRowsInSection:(NSInteger)section
{return _objects.count;}// Customize the appearance of table view cells.-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath
{staticNSString*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
{// 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:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];}elseif(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 =[[NSDateFormatter alloc] init];[formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss zzz"];NSString*stringFromDate =[formatter stringFromDate:object];self.detailViewController.detailDescriptionLabel.text = stringFromDate;}@end

8. DetailViewController.h 内容如下:

#import <UIKit/UIKit.h>@interfaceDetailViewController:UIViewController<UISplitViewControllerDelegate>@property(strong, nonatomic) id detailItem;@property(weak, nonatomic)IBOutletUILabel*detailDescriptionLabel;@end

9. DetailViewController.m 内容如下:

#import "DetailViewController.h"@interfaceDetailViewController()@property(strong, nonatomic)UIPopoverController*masterPopoverController;-(void)configureView;@end@implementationDetailViewController#pragma mark -Managing the detail item

-(void)setDetailItem:(id)newDetailItem
{if(_detailItem != newDetailItem){
        _detailItem = newDetailItem;// Update the view.[self configureView];}if(self.masterPopoverController !=nil){[self.masterPopoverController dismissPopoverAnimated:YES];}}-(void)configureView
{// Update the user interface for the detail item.if(self.detailItem){self.detailDescriptionLabel.text =[self.detailItem description];}}-(void)viewDidLoad
{[super viewDidLoad];[self configureView];}-(void)didReceiveMemoryWarning
{[super didReceiveMemoryWarning];// Dispose of any resources that can be recreated.}-(id)initWithNibName:(NSString*)nibNameOrNil bundle:(NSBundle*)nibBundleOrNil
{self=[super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];if(self){self.title =NSLocalizedString(@"Detail",@"Detail");}returnself;}#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
{// Called when the view is shown again in the split view, //invalidating the button and popover controller.[self.navigationItem setLeftBarButtonItem:nil animated:YES];self.masterPopoverController =nil;}@end

10. 现在,当我们运行程序,在横向模式下,我们将得到下面的输出。

iOS Tutorial

11. 我们将得到下面的输出,当我们切换到纵向模式下。

iOS Tutorial

 
 
 
本站文章除注明转载外,均为本站原创或编译

欢迎任何形式的转载,但请务必注明出处,尊重他人劳动,传播学习教程;

转载请注明:文章转载自:易百教程 [http://www.yiibai.com]

本文标题:iOS – Split View(分割视图)


转载请保留原文链接:http://www.yiibai.com/html/ios/2013/0901221.html

免责声明:本站所有文章内容,图片,视频等均是来源于用户投稿和互联网及文摘转载整编而成,不代表本站观点,不承担相关法律责任。其著作权各归其原作者或其出版社所有。如发现本站有涉嫌抄袭侵权/违法违规的内容,侵犯到您的权益,请在线联系站长,一经查实,本站将立刻删除。 本文来自网络,若有侵权,请联系删除,如若转载,请注明出处:https://yundeesoft.com/34378.html

(0)

相关推荐

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注微信