iPhone和iPad都使用同一个操作系统——ios,因此,它们的API基本上是一样的,但有一些是iPad专用的,比如UISplitViewController控制器,UISplitViewController控制器用于呈现“屏幕分栏”类型的视图。UISplitViewController有两个视图控制器,一个是MasterViewController,另一个是DetailViewController,iPad在横屏的情况下,两者都显示出来;iPad在竖屏的情况下,MasterViewController隐藏,只有DetailViewController显示出来。
代码实现如下:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
MasterViewController *masterViewCtl = [[MasterViewController alloc] init];
DetailViewController *detailViewCtl = [[DetailViewController alloc] init];
UINavigationController *navCtl = [[UINavigationController alloc] initWithRootViewController:masterViewCtl];
_splitViewCtl = [[KFSplitViewController alloc] init];
[_splitViewCtl addChildViewController:navCtl];
[_splitViewCtl addChildViewController:detailViewCtl];
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.window.backgroundColor = [UIColor whiteColor];
self.window.rootViewController = _splitViewCtl;
[self.window makeKeyAndVisible];
[masterViewCtl release];
[detailViewCtl release];
[navCtl release];
[_splitViewCtl release];
return YES;
}
//
// KFSplitViewController.h
//
#import <UIKit/UIKit.h>
@interface KFSplitViewController : UISplitViewController
@end
//
// KFSplitViewController.m
//
#import "KFSplitViewController.h"
@interface KFSplitViewController ()
@end
@implementation KFSplitViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self)
{
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
}
@end
//
// MasterViewController.h
//
#import <UIKit/UIKit.h>
@interface MasterViewController : UITableViewController
{
NSMutableArray *mutArr;
int iSelect;
}
@end
//
// MasterViewController.m
//
#import "MasterViewController.h"
@interface MasterViewController ()
@end
@implementation MasterViewController
- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
mutArr = [[NSMutableArray alloc] initWithCapacity:20];
for (int i = 0; i < 10; i ++)
{
NSString *str = [NSString stringWithFormat:@"Cell_%d",i];
[mutArr addObject:str];
}
self.title = @"Master View";
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 10;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *indentifer = @"myCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:indentifer];
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:indentifer] autorelease];
}
int iRow = [indexPath row];
NSString *str = [mutArr objectAtIndex:iRow];
cell.textLabel.text = str;
if (iSelect == iRow)
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
else
cell.accessoryType = UITableViewCellAccessoryNone;
return cell;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
iSelect = [indexPath row];
NSString *str = [mutArr objectAtIndex:iSelect];
[[NSNotificationCenter defaultCenter] postNotificationName:@"MasterMessageToDetail" object:str userInfo:nil];
[tableView reloadData];
}
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
return YES;
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete)
{
} else if (editingStyle == UITableViewCellEditingStyleInsert) {
}
}
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath
{
}
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
return YES;
}
@end
//
// DetailViewController.h
//
#import <UIKit/UIKit.h>
@interface DetailViewController : UIViewController
{
UILabel *label;
}
@end
//
// DetailViewController.m
//
#import "DetailViewController.h"
@interface DetailViewController ()
@end
@implementation DetailViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self)
{
}
return self;
}
- (void)dealloc
{
// 取消观察者
[[NSNotificationCenter defaultCenter] removeObserver:self];
[super dealloc];
}
- (void)viewDidLoad
{
[super viewDidLoad];
// 注册观察者
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handl:) name:@"MasterMessageToDetail" object:nil];
[self initButton];
}
- (void)initButton
{
label = [[UILabel alloc] initWithFrame:CGRectMake(300, 200, 150, 40)];
label.backgroundColor = [UIColor whiteColor];
label.layer.cornerRadius = 5.0f;
label.layer.masksToBounds = 5.0f;
label.textAlignment = NSTextAlignmentCenter;
label.textColor = [UIColor blackColor];
[self.view addSubview:label];
[label release];
UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(300, 250, 150, 40)];
btn.backgroundColor = [UIColor whiteColor];
btn.layer.cornerRadius = 5.0f;
btn.showsTouchWhenHighlighted = YES;
[btn setTitle:@"Detail View" forState:UIControlStateNormal];
[btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[self.view addSubview:btn];
[btn release];
}
- (void)handl:(NSNotification *)notifi
{
NSString *str = [notifi object];
label.text = str;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
}
@end
至此,iPad专用API(UISplitViewController)控制器介绍已经完毕,程序运行效果图如下:
iPad 横屏效果图:
iPad 竖屏效果图: