その実装方法。
1. 新規プロジェクトの作成
xibを使わないとはいえ、コードはSplit View-based Applicationのテンプレートを選んで作ったプロジェクトのコードを流用でき、生産性も上がるので、Split View-based Applicationのテンプレートから新規プロジェクトを作成する。
2. MainWindow.xibを削除する。
3. info.plistを変更。
Main nib file base nameの項目を削除する。
4. main.m
・変更前
#import <UIKit/UIKit.h>
int main(int argc, char *argv[])
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
int retVal = UIApplicationMain(argc, argv, nil, nil);
[pool release];
return retVal;
}
・変更後
#import <UIKit/UIKit.h>アプリケーションの開始がAppDelegateであることを明示する。
int main(int argc, char *argv[])
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
int retVal = UIApplicationMain(argc, argv, nil, @"AppDelegate");
[pool release];
return retVal;
}
5. AppDelegate.h は、変更なし。
#import <UIKit/UIKit.h>
@class RootViewController;
@class DetailViewController;
@interface AppDelegate : NSObject <UIApplicationDelegate>
@property (nonatomic, retain) IBOutlet UIWindow *window;
@property (nonatomic, retain, readonly) NSManagedObjectContext *managedObjectContext;
@property (nonatomic, retain, readonly) NSManagedObjectModel *managedObjectModel;
@property (nonatomic, retain, readonly) NSPersistentStoreCoordinator *persistentStoreCoordinator;
- (void)saveContext;
- (NSURL *)applicationDocumentsDirectory;
@property (nonatomic, retain) IBOutlet UISplitViewController *splitViewController;
@property (nonatomic, retain) IBOutlet RootViewController *rootViewController;
@property (nonatomic, retain) IBOutlet DetailViewController *detailViewController;
@end
6. AppDelegate.m
・変更前
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// Override point for customization after application launch.
// Add the split view controller's view to the window and display.
self.window.rootViewController = self.splitViewController;
[self.window makeKeyAndVisible];
return YES;
}
・変更後
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions(1) rootViewController、detailViewController、navigationControllerを作る。
{
_window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
RootViewController *rootViewController = [[RootViewController alloc] init];
UINavigationController* navigationController = [[UINavigationController alloc] initWithRootViewController:rootViewController];
DetailViewController *detailViewController = [[DetailViewController alloc] init];
rootViewController.detailViewController = detailViewController;
UISplitViewController *splitViewController = [[UISplitViewController alloc] init];
splitViewController.viewControllers = [NSArray arrayWithObjects:navigationController, detailViewController, nil];
splitViewController.delegate = detailViewController;
[_window addSubview:splitViewController.view];
[rootViewController release];
[navigationController release];
[detailViewController release];
[_window makeKeyAndVisible];
return YES;
}
(2) rootViewController内のdetailViewControllerに、detailViewControllerのポインタを設定。
(3) splitViewControllerを作り、その配列splitViewController.viewControllersに、navigationController, detailViewControllerのポインタを設定。配列に入れる順番により挙動が変わるので、この通りにする。
(4) splitViewController.delegateに、detailViewControllerのポインタを設定。
(5) _windowにsplitViewControllerをaddSubViewして、_windowを表示。
7. RootViewController.h (左ペイン)
・変更前
#import <UIKit/UIKit.h>
@class DetailViewController;
#import <CoreData/CoreData.h>
@interface RootViewController : UITableViewController <NSFetchedResultsControllerDelegate>
@property (nonatomic, retain) IBOutlet DetailViewController *detailViewController;
@property (nonatomic, retain) NSFetchedResultsController *fetchedResultsController;
@property (nonatomic, retain) NSManagedObjectContext *managedObjectContext;
- (void)insertNewObject:(id)sender;
@end
・変更後
#import <UIKit/UIKit.h>IBOutletを削除する。
@class DetailViewController;
#import <CoreData/CoreData.h>
@interface RootViewController : UITableViewController <NSFetchedResultsControllerDelegate>
@property (nonatomic, retain) DetailViewController *detailViewController;
@property (nonatomic, retain) NSFetchedResultsController *fetchedResultsController;
@property (nonatomic, retain) NSManagedObjectContext *managedObjectContext;
- (void)insertNewObject:(id)sender;
@end
8. DetailViewController.h(右ペイン)
・変更前
#import <UIKit/UIKit.h>
#import <CoreData/CoreData.h>
@class RootViewController;
@interface DetailViewController : UIViewController <UIPopoverControllerDelegate, UISplitViewControllerDelegate>
@property (nonatomic, retain) IBOutlet UIToolbar *toolbar;
@property (nonatomic, retain) NSManagedObject *detailItem;
@property (nonatomic, retain) IBOutlet UILabel *detailDescriptionLabel;
@property (nonatomic, assign) IBOutlet RootViewController *rootViewController;
- (IBAction)insertNewObject:(id)sender;
@end
・変更後
#import <UIKit/UIKit.h>
#import <CoreData/CoreData.h>
@class RootViewController;
@interface DetailViewController : UIViewController <UIPopoverControllerDelegate, UISplitViewControllerDelegate>
@property (nonatomic, retain) UIToolbar *toolbar;
@property (nonatomic, retain) NSManagedObject *detailItem;
@property (nonatomic, retain) UILabel *detailDescriptionLabel;
@property (nonatomic, assign) IBOutlet RootViewController *rootViewController;
- (IBAction)insertNewObject:(id)sender;
@end
・追加
- (id)init {
if (self = [super init]) {
self.view.backgroundColor = [UIColor whiteColor];
_toolbar = [[UIToolbar alloc] init];
[_toolbar sizeToFit];
[self.view addSubview:_toolbar];
[_toolbar setItems: [NSArray array]];
_detailDescriptionLabel = [[UILabel alloc] initWithFrame: CGRectMake(0, self.view.bounds.size.height * 0.5, self.view.bounds.size.width, 17)];
_detailDescriptionLabel.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin;
_detailDescriptionLabel.textAlignment = UITextAlignmentCenter;
_detailDescriptionLabel.text = @"Detail view content goes here";
[self.view addSubview:_detailDescriptionLabel];
}
return self;
}
■参考記事
http://blog.bornneet.com/Entry/288/
【UISplitViewの最新記事】