UISplitViewController用法

  公司要做ipad客户端了,所以研究了ipad下才有的UISplitViewController

  UISplitViewController 只能作为window的rootViewController,所以我们可以先自定义个RootViewController继承UISplitViewController

#import <UIKit/UIKit.h>

@interface RootViewController : UISplitViewController

@end

- (void)viewDidLoad

{

    [superviewDidLoad];

    MenuTableViewController *MenuTVC = [[MenuTableViewControlleralloc] init];

    DetailViewController *detailVC = [[DetailViewControlleralloc] init];

    self.viewControllers = [NSArrayarrayWithObjects:MenuTVC,detailVC, nil];

self.delegate = detailVC;

}

  然后分别定义SplitViewController的列表页MenuTableViewController

#import <UIKit/UIKit.h>

@interface MenuTableViewController :UITableViewController{

NSArray *_arr;

}

@property(nonatomic,retain)NSArray *arr;

@end

#import "MenuTableViewController.h"

#import "DetailViewController.h"

@implementation MenuTableViewController

@synthesize arr=_arr;

- (id)initWithStyle:(UITableViewStyle)style

{

self = [super initWithStyle:style];

if (self) {

        self.arr=[NSArrayarrayWithObjects:@"1",@"2",@"3",@"4",@"5",@"6",@"7", nil];

    }

    returnself;

}

- (void)didReceiveMemoryWarning

{

    // Releases the view if it doesn't have a superview.

    [superdidReceiveMemoryWarning];

    // Release any cached data, images, etc that aren't in use.

}

#pragma mark - View lifecycle

- (void)viewDidLoad

{

    [superviewDidLoad];

}

- (void)viewDidUnload

{

    [superviewDidUnload];

    // Release any retained subviews of the main view.

    // e.g. self.myOutlet = nil;

}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation

{

    // Return YES for supported orientations

returnYES;

}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView

{

    // Return the number of sections.

return 1;

}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section

{

    // Return the number of rows in the section.

    returnself.arr.count;

}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

{

static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

if (cell == nil) {

        cell = [[[UITableViewCellalloc] initWithStyle:UITableViewCellStyleDefaultreuseIdentifier:CellIdentifier] autorelease];

    }

    // Configure the cell...

    cell.textLabel.text=[ self.arrobjectAtIndex:indexPath.row];

return cell;

}

#pragma mark - Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath

{

    DetailViewController *detailVC=[self.splitViewController.viewControllersobjectAtIndex:1];

    detailVC.lable.text=[self.arr objectAtIndex:indexPath.row];

    [detailVC.popdismissPopoverAnimated:YES];

    [detailVC release];

    [MenuTVC release];

 

}

@end

  正文页DetailViewController

#import <UIKit/UIKit.h>

#import <CommonCrypto/CommonDigest.h>

@interface DetailViewController : UIViewController<UISplitViewControllerDelegate>{

    

    UILabel *_lable;

    

    UIToolbar *_toolBar;

    UIPopoverController *_pop;

}

@property(nonatomic,retain)UILabel *lable;

@property(nonatomic,retain)UIToolbar *toolBar;

@property(nonatomic,retain)UIPopoverController *pop;

@end

#import "DetailViewController.h"

@implementation DetailViewController

@synthesize lable=_lable,toolBar=_toolBar,pop=_pop;

 

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil

{

    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];

    if (self) {

        // Custom initialization

    }

    returnself;

}

 

- (void)didReceiveMemoryWarning

{

    // Releases the view if it doesn't have a superview.

    [superdidReceiveMemoryWarning];

    

    // Release any cached data, images, etc that aren't in use.

}

 

#pragma mark - View lifecycle

 

- (void)viewDidLoad

{

    [superviewDidLoad];

    // Do any additional setup after loading the view from its nib.

    self.lable=[[[UILabelalloc] initWithFrame:CGRectMake(100, 200, 50, 50)] autorelease];

    

    self.view.backgroundColor=[UIColorredColor];

    [self.viewaddSubview:self.lable];

 

}

 

 

- (void)viewDidUnload

{

    [superviewDidUnload];

    // Release any retained subviews of the main view.

    // e.g. self.myOutlet = nil;

}

 

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation

{

    // Return YES for supported orientations

returnYES;

}

- (void)splitViewController: (UISplitViewController*)svc willHideViewController:(UIViewController *)aViewController withBarButtonItem:(UIBarButtonItem*)barButtonItem forPopoverController: (UIPopoverController*)pc{

    

   

self.toolBar=[[[UIToolbaralloc] initWithFrame:CGRectMake(0, 0, 768, 50)] autorelease];

    barButtonItem.title=@"菜单";

    UIViewController *two=[svc.viewControllersobjectAtIndex:1];

    

    [two.view addSubview:self.toolBar];

    [self.toolBarsetItems:[NSArrayarrayWithObject:barButtonItem]

                  animated:YES];

    self.pop=pc;

    

    

    

}

 

// Called when the view is shown again in the split view, invalidating the button and popover controller

- (void)splitViewController: (UISplitViewController*)svc willShowViewController:(UIViewController *)aViewController invalidatingBarButtonItem:(UIBarButtonItem *)barButtonItem{

    

    if (self.toolBar!=nil) {

        [self.toolBarremoveFromSuperview];

    }

    

    

}

 @end

posted @ 2012-09-18 18:00  七夜小魔军  阅读(538)  评论(0编辑  收藏  举报