2010/09/12

[iPad][Objective-C]splitViewを表示する

iPadの新機能であるspliteViewを表示させる方法について書きたいと思います。

//sqliteBrowserAppDelegate.h

#import <UIKit/UIKit.h>

#import "MainSplitViewController.h"
#import "LeftNavigationController.h"
#import "LeftTableViewController.h"
#import "RightNavigationController.h"
#import "RightViewController.h"

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

@property (nonatomic, retain) IBOutlet UIWindow *window;

@end

//sqliteBrowserAppDelegate.m
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

LeftTableViewController *lefttableviewcontroller = [[LeftTableViewController alloc] initWithStyle:UITableViewStylePlain];
LeftNavigationController *leftnavigationcontroller = [[LeftNavigationController alloc]
initWithRootViewController:lefttableviewcontroller
];

RightViewController *rightviewcontroller = [[RightViewController alloc] initWithNibName:nil bundle:nil];
RightNavigationController *rightnavigationcontroller = [[RightNavigationController alloc]
initWithRootViewController:rightviewcontroller
];

MainSplitViewController *mainsplitviewcontroller = [[MainSplitViewController alloc] init];
mainsplitviewcontroller.viewControllers = [NSArray arrayWithObjects:leftnavigationcontroller,rightnavigationcontroller,nil];

[window addSubview:mainsplitviewcontroller.view];
[window makeKeyAndVisible];

return YES;
}

//MainSplitViewController.h
#import <UIKit/UIKit.h>

@interface MainSplitViewController : UISplitViewController {

}

@end

//MainSplitViewController.m
#import "MainSplitViewController.h"


@implementation MainSplitViewController


- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Overriden to allow any orientation.
return YES;
}


- (void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];

// Release any cached data, images, etc that aren't in use.
}


- (void)viewDidUnload {
[super viewDidUnload];
}


- (void)dealloc {
[super dealloc];
}


@end

//LeftNavigationController.h
#import <UIKit/UIKit.h>


@interface LeftNavigationController : UINavigationController {

}

@end

//LeftNavigationController.m
#import "LeftNavigationController.h"


@implementation LeftNavigationController


- (id)initWithRootViewController:(UIViewController *)rootViewController{
if((self = [super initWithRootViewController:rootViewController])){

}
return self;
}


- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Overriden to allow any orientation.
return YES;
}


- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];

}


- (void)viewDidUnload {
[super viewDidUnload];
}


- (void)dealloc {
[super dealloc];
}


@end

//LeftTableViewController.h
#import <UIKit/UIKit.h>

@interface LeftTableViewController : UITableViewController {

}

@end

//LeftTableViewController.m
#import "LeftTableViewController.h"


@implementation LeftTableViewController


#pragma mark -
#pragma mark Initialization

- (id)initWithStyle:(UITableViewStyle)style {
if ((self = [super initWithStyle:style])) {
UIBarButtonItem *addbutton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd
target:nil
action:nil
];
self.navigationItem.leftBarButtonItem = addbutton;
}
return self;
}



#pragma mark -
#pragma mark View lifecycle


- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
return YES;
}


#pragma mark -
#pragma mark Table view data source

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


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

- (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...

return cell;
}

#pragma mark -
#pragma mark Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

}


#pragma mark -
#pragma mark Memory management

- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];

}

- (void)viewDidUnload {
}


- (void)dealloc {
[super dealloc];
}


@end

//RightNavigationController.h
#import <UIKit/UIKit.h>


@interface RightNavigationController : UINavigationController {

}

@end

//RightNavigationController.m
#import "RightNavigationController.h"


@implementation RightNavigationController


- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
return YES;
}


- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}


- (void)viewDidUnload {
[super viewDidUnload];
}


- (void)dealloc {
[super dealloc];
}


@end

//RightViewController.h
#import <UIKit/UIKit.h>


@interface RightViewController : UIViewController {

}

@end

//RightViewController.m
#import "RightViewController.h"


@implementation RightViewController


- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
if ((self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil])) {
self.view = [[UIView alloc] initWithFrame:CGRectMake(320, 0, 704, 768)];
self.view.backgroundColor = [UIColor whiteColor];
}
return self;
}


- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
return YES;
}


- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}


- (void)viewDidUnload {
[super viewDidUnload];
}


- (void)dealloc {
[super dealloc];
}


@end

実行結果は、このような感じになります。
Photobucket
特に重要なのが、UISplitViewControllerのインスタンス変数を作る際に、initをコールするということ。

実際にスプリット元になるviewcontrollerに関しては、その後の、viewControllersプロパティーで設定するということさえ押さえておけば、難しいことはありません。

こうして一度、まとめておけば、次も楽に作成することができるぞと。

ちなみに、参考にした箇所は、iPad Programming GuideのCreating a Split View Interfaceというところ。


0 コメント:

コメントを投稿