iPhone之UISplitViewController

转自:http://blog.sina.com.cn/s/blog_4adf31ea0100qxoh.html

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

    ipad的屏幕比iphone大,所以在界面上,ipad比iphone多一个UISplitViewController,用来实现ipad在横屏时,分两栏显示所需要的界面,可以一边是目录一边是具体的内容。
iPhone之UISplitViewController_第1张图片


    首先创建一个 Row1DetailRootViewController,其中RootViewController继承UITableViewController。同事创建两个相应的xib文件。


AppDelegate 中的代码如下:

@interfacetestsplitviewAppDelegate : 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 *)applicationdidFinishLaunchingWithOptions:(NSDictionary *)launchOptions {   

    

   // Override point for customization afterapp launch. 

   [self.windowaddSubview:splitView.view];

   [self.windowmakeKeyAndVisible];


return YES;

}



修改MainWindow.xib文件:添加UISplitViewController容器



绑定的事件如下:
iPhone之UISplitViewController_第2张图片

RootViewController的代码如下:


 

@interface RootViewController :UITableViewController {

Row1Detail *detail;

}


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


@end



实现代码:


 

#import"RootViewController.h"



@implementationRootViewController

@synthesize detail;


#pragma mark -

#pragma mark View lifecycle


-(void)viewDidLoad {

   [superviewDidLoad];

   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 *)aTableViewnumberOfRowsInSection:(NSInteger)section {

    return 10;

}



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

    

    static NSString *CellIdentifier = @"CellIdentifier";

    

   UITableViewCell *cell = [tableViewdequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil) {

       cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefaultreuseIdentifier: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 *)aTableViewdidSelectRowAtIndexPath:(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:selfcancelButtonTitle:@""otherButtonTitles:nil];

[alert show];

 

[alert release];

 

Text1.text=@"11111111111";

}


-(void)setDetailItem:(id)newDetailItem 

{

    if (detailItem != newDetailItem) {

       [detailItemrelease];

       detailItem =[newDetailItem retain];

Text1.text =[detailItem description];


    }

     

}


// Implement viewDidLoad to do additional setup after loading theview, typically from a nib.

-(void)viewDidLoad 

{

   [superviewDidLoad];

}




-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation{

   // Overriden to allow anyorientation.

   return YES;

}


你可能感兴趣的:(iPhone之UISplitViewController)