UISplitViewController

本文介绍如何在iPad应用中使用UISplitViewController实现分屏显示功能。通过创建RootViewController和Row1Detail视图控制器,并结合AppDelegate配置,展示了如何在横屏模式下实现一栏为目录另一栏为内容的具体实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

UISplitViewController类,只有在ipad中才可以使用。

ipad的屏幕比iphone大,所以在界面上,ipad比iphone多一个UISplitViewController,用来实现ipad在横屏时,分两栏显示所需要的界面,可以一边是目录一边是具体的内容[img]http://s14.sinaimg.cn/middle/4adf31eat9e1ad173199d&690[/img]
首先创建一个Row1Detail和RootViewController,其中RootViewController继承UITableViewController。同事创建两个相应的xib文件。


AppDelegate 中的代码如下:

@interface testsplitviewAppDelegate : NSObject <UIApplicationDelegate> {
UIWindow *window;

UISplitViewController *splitView;
RootViewController *root;
Row1Detail *detail;

}

@property (nonatomic, retain) IBOutlet UIWindow *window;
@property (nonatomic, retain) IBOutlet UISplitViewController *splitView;
@property (nonatomic,retain) IBOutlet RootViewController *root;
@property (nonatomic, retain) IBOutlet Row1Detail *detail;

@end


实现类:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

// Override point for customization after app launch.
[self.window addSubview:splitView.view];
[self.window makeKeyAndVisible];

return YES;
}

修改MainWindow.xib文件:添加UISplitViewController容器
[img]http://s5.sinaimg.cn/middle/4adf31eat9e1ae992b4a4&690[/img]
绑定的事件如下:
[img]http://s9.sinaimg.cn/middle/4adf31eat9e1aed63e078&690[/img]
RootViewController的代码如下:

@interface RootViewController : UITableViewController {
Row1Detail *detail;
}

@property (nonatomic,retain) IBOutlet Row1Detail *detail;

@end


实现代码:


#import "RootViewController.h"


@implementation RootViewController
@synthesize detail;

#pragma mark -
#pragma mark View lifecycle

- (void)viewDidLoad {
[super viewDidLoad];
self.clearsSelectionOnViewWillAppear = NO;
self.contentSizeForViewInPopover = CGSizeMake(320.0, 600.0);
}


#pragma mark -
#pragma mark Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)aTableView {
return 1;
}


- (NSInteger)tableView:(UITableView *)aTableView numberOfRowsInSection:(NSInteger)section {
return 10;
}


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

static NSString *CellIdentifier = @"CellIdentifier";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
cell.accessoryType = UITableViewCellAccessoryNone;
}

cell.textLabel.text = [NSString stringWithFormat:@"Row %d", indexPath.row];
return cell;
}


#pragma mark -
#pragma mark Table view delegate

- (void)tableView:(UITableView *)aTableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
detail.detailItem = [NSString stringWithFormat:@"Row %d", indexPath.row];
}





Row1Detail的代码


@interface Row1Detail : UIViewController <UISplitViewControllerDelegate>{
UILabel *Text1;

id detailItem;
}

@property (nonatomic,retain) IBOutlet UILabel *Text1;

@property (nonatomic, retain) id detailItem;

-(IBAction) click;

@end


实现代码如下:



#import "Row1Detail.h"


@implementation Row1Detail
@synthesize Text1;
@synthesize detailItem;


-(IBAction) click
{
NSLog(@"%@",Text1.text);

UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"标题" message:@"22" delegate:self cancelButtonTitle:@"关" otherButtonTitles:nil];
[alert show];

[alert release];

Text1.text=@"11111111111";
}

- (void)setDetailItem:(id)newDetailItem
{
if (detailItem != newDetailItem) {
[detailItem release];
detailItem = [newDetailItem retain];
Text1.text = [detailItem description];

}

}

// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad
{
[super viewDidLoad];
}


- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Overriden to allow any orientation.
return YES;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值