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">ios之UISplitViewController-LMLPHP

然后创建一个DetailViewController和RootViewController,其中RootViewController继承UITableViewController。同事创建两个相应的xib文件。删除ipad_demoViewController.相应的类列表如下:

<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">ios之UISplitViewController-LMLPHP

然后修改ipad_demoAppDelegate:

.h文件:

  1. #import <UIKit/UIKit.h>
  2. #import "RootViewController.h"
  3. #import "DetailViewController.h"
  4. @class ipad_demoViewController;
  5. @interface ipad_demoAppDelegate : NSObject <UIApplicationDelegate> {
  6. UIWindow *window;
  7. UISplitViewController *splitViewController;
  8. RootViewController *rootViewController;
  9. DetailViewController *detailViewController;
  10. }
  11. @property (nonatomic, retain) IBOutlet UIWindow *window;
  12. @property (nonatomic, retain) IBOutlet UISplitViewController *splitViewController;
  13. @property (nonatomic, retain) IBOutlet RootViewController *rootViewController;
  14. @property (nonatomic, retain)  IBOutlet DetailViewController *detailViewController;
  15. @end

复制代码

.m文件:

  1. - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
  2. // Override point for customization after app launch.
  3. [window addSubview:splitViewController.view];
  4. [window makeKeyAndVisible];
  5. return YES;
  6. }

复制代码

修改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">ios之UISplitViewController-LMLPHP

IB中的控件和相应的事件相联系:连接过程是按住control键,然后点击ipad_demo App Delegate,连接到Split View Controller,然后选择自定义的事件即可,最后的结果如下:

<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">ios之UISplitViewController-LMLPHP

最后在把相应的容器换成自定义的容器:实现的方法是

<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">ios之UISplitViewController-LMLPHP

最后的结果是:

<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">ios之UISplitViewController-LMLPHP

现在我们在实现DetailViewController:

首先修改其相应的文件:
.h文件修改如下:

  1. #import <UIKit/UIKit.h>
  2. @interface DetailViewController : UIViewController {
  3. IBOutlet UILabel *lable;
  4. IBOutlet UISwitch *switch1;
  5. NSIndexPath *index;
  6. }
  7. -(void)deetail:(id)sender;
  8. @property (nonatomic,retain) UILabel *lable;
  9. @property (nonatomic,retain) UISwitch *switch1;
  10. @end

复制代码

.m文件修改如下:

  1. #import "DetailViewController.h"
  2. @implementation DetailViewController
  3. @synthesize lable,switch1;
  4. - (void)viewDidLoad {
  5. [super viewDidLoad];
  6. }
  7. -(void)viewWillAppear:(BOOL)animated
  8. {
  9. }
  10. -(void)deetail:(id)sender
  11. {
  12. index=sender;
  13. self.lable.text=[NSString stringWithFormat:@"Row %d,section %d",[index row],[index section]];
  14. if ([index row]%2==0) {
  15. self.switch1.on=YES;
  16. }else {
  17. self.switch1.on=NO;
  18. }
  19. }
  20. - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
  21. return YES;
  22. }
  23. - (void)didReceiveMemoryWarning {
  24. [super didReceiveMemoryWarning];
  25. }
  26. - (void)viewDidUnload {
  27. [super viewDidUnload];
  28. self.lable=nil;
  29. self.switch1=nil;
  30. }
  31. - (void)dealloc {
  32. [self.lable release];
  33. [self.switch1 release];
  34. [super dealloc];
  35. }
  36. @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">ios之UISplitViewController-LMLPHP

最后我们实现RootViewController:

首先修改相应的文件:
.h文件如下:

  1. #import <UIKit/UIKit.h>
  2. @class DetailViewController;
  3. @interface RootViewController : UITableViewController {
  4. IBOutlet DetailViewController *detailViewController;
  5. }
  6. @property (nonatomic,retain) DetailViewController *detailViewController;
  7. @end

复制代码

.m文件如下:

  1. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
  2. // Return the number of sections.
  3. return 2;
  4. }
  5. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  6. // Return the number of rows in the section.
  7. return 7;
  8. }
  9. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  10. static NSString *CellIdentifier = @"Cell";
  11. UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
  12. if (cell == nil) {
  13. cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
  14. }
  15. // Configure the cell…
  16. cell.textLabel.text=[NSString stringWithFormat:@"ROW %d section %d",[indexPath row],[indexPath section]];
  17. return cell;
  18. }
  19. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
  20. // Navigation logic may go here. Create and push another view controller.
  21. // DetailViewController *detailViewController = [[DetailViewController alloc] initWithNibName:@"DetailViewController" bundle:nil];
  22. // [self.navigationController pushViewController:detailViewController animated:YES];
  23. // [detailViewController release];
  24. [detailViewController deetail:indexPath];
  25. }

复制代码

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">ios之UISplitViewController-LMLPHP

运行结果如下:

<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">ios之UISplitViewController-LMLPHP

2011-3-27 14:53:48 上传

下载附件 (43.59 KB)

 

源代码: http://easymorse-iphone.googlecode.com/svn/trunk/ipad.demo/

05-28 23:27