iPad的屏幕比iPhone大,所以在界面上,iPad比iPhone多一个UISplitViewController,用来实现iPad在横屏时,分两栏显示所需要的界面,可以一边是目录一边是具体的内容。下面我将详细的阐述UISplitViewController在ipad中的使用。
首先是创建一个工程:iPad.demo.
<IGNORE_JS_OP style="WIDOWS: 2; TEXT-TRANSFORM: none; BACKGROUND-COLOR: rgb(255,255,255); TEXT-INDENT: 0px; FONT: 14px/21px Tahoma, 'Microsoft Yahei', Simsun; WORD-WRAP: break-word; WHITE-SPACE: normal; ORPHANS: 2; LETTER-SPACING: normal; COLOR: rgb(68,68,68); WORD-SPACING: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px">
.h文件:
#import <UIKit/UIKit.h>
#import "RootViewController.h"
#import "DetailViewController.h"
@class ipad_demoViewController;
@interface ipad_demoAppDelegate : NSObject <UIApplicationDelegate> {
UIWindow *window;
UISplitViewController *splitViewController;
RootViewController *rootViewController;
DetailViewController *detailViewController;
}
@property (nonatomic, retain) IBOutlet UIWindow *window;
@property (nonatomic, retain) IBOutlet UISplitViewController *splitViewController;
@property (nonatomic, retain) IBOutlet RootViewController *rootViewController;
@property (nonatomic, retain) IBOutlet DetailViewController *detailViewController;
@end
复制代码
.m文件:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after app launch.
[window addSubview:splitViewController.view];
[window makeKeyAndVisible];
return YES;
}
复制代码
修改MainWindow.xib文件:
添加UISplitViewController容器:
<IGNORE_JS_OP style="WIDOWS: 2; TEXT-TRANSFORM: none; BACKGROUND-COLOR: rgb(255,255,255); TEXT-INDENT: 0px; FONT: 14px/21px Tahoma, 'Microsoft Yahei', Simsun; WORD-WRAP: break-word; WHITE-SPACE: normal; ORPHANS: 2; LETTER-SPACING: normal; COLOR: rgb(68,68,68); WORD-SPACING: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px">
.h文件修改如下:
#import <UIKit/UIKit.h>
@interface DetailViewController : UIViewController {
IBOutlet UILabel *lable;
IBOutlet UISwitch *switch1;
NSIndexPath *index;
}
-(void)deetail:(id)sender;
@property (nonatomic,retain) UILabel *lable;
@property (nonatomic,retain) UISwitch *switch1;
@end
复制代码
.m文件修改如下:
#import "DetailViewController.h"
@implementation DetailViewController
@synthesize lable,switch1;
- (void)viewDidLoad {
[super viewDidLoad];
}
-(void)viewWillAppear:(BOOL)animated
{
}
-(void)deetail:(id)sender
{
index=sender;
self.lable.text=[NSString stringWithFormat:@"Row %d,section %d",[index row],[index section]];
if ([index row]%2==0) {
self.switch1.on=YES;
}else {
self.switch1.on=NO;
}
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
return YES;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
- (void)viewDidUnload {
[super viewDidUnload];
self.lable=nil;
self.switch1=nil;
}
- (void)dealloc {
[self.lable release];
[self.switch1 release];
[super dealloc];
}
@end
复制代码
2.修改相应的xib文件:
添加相应的控件,并且相应的组建和相应的事件相关联。
<IGNORE_JS_OP style="WIDOWS: 2; TEXT-TRANSFORM: none; BACKGROUND-COLOR: rgb(255,255,255); TEXT-INDENT: 0px; FONT: 14px/21px Tahoma, 'Microsoft Yahei', Simsun; WORD-WRAP: break-word; WHITE-SPACE: normal; ORPHANS: 2; LETTER-SPACING: normal; COLOR: rgb(68,68,68); WORD-SPACING: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px">
.h文件如下:
#import <UIKit/UIKit.h>
@class DetailViewController;
@interface RootViewController : UITableViewController {
IBOutlet DetailViewController *detailViewController;
}
@property (nonatomic,retain) DetailViewController *detailViewController;
@end
复制代码
.m文件如下:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
// Return the number of sections.
return 2;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
// Return the number of rows in the section.
return 7;
}
- (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] autorelease];
}
// Configure the cell…
cell.textLabel.text=[NSString stringWithFormat:@"ROW %d section %d",[indexPath row],[indexPath section]];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
// Navigation logic may go here. Create and push another view controller.
// DetailViewController *detailViewController = [[DetailViewController alloc] initWithNibName:@"DetailViewController" bundle:nil];
// [self.navigationController pushViewController:detailViewController animated:YES];
// [detailViewController release];
[detailViewController deetail:indexPath];
}
复制代码
2、修改相应的xib文件:
相应的事件和控件相联系。
<IGNORE_JS_OP style="WIDOWS: 2; TEXT-TRANSFORM: none; BACKGROUND-COLOR: rgb(255,255,255); TEXT-INDENT: 0px; FONT: 14px/21px Tahoma, 'Microsoft Yahei', Simsun; WORD-WRAP: break-word; WHITE-SPACE: normal; ORPHANS: 2; LETTER-SPACING: normal; COLOR: rgb(68,68,68); WORD-SPACING: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px">
源代码: http://easymorse-iphone.googlecode.com/svn/trunk/ipad.demo/