タブ画面(UITabBarController)の下層にナビゲーション画面(UINavigationController)を配置するという形になる。
従って、既にNavigation-based Applicationベースでアプリケーションを開発している場合、新たにTab Bar Applicationで開発しなおす必要がある。
但し、既にNavigation-based Applicationベースで開発したアプリケーションの大部分は流用することができる。
下記の手順は既にNavigation-based Applicationベースで開発したアプリケーションがあることを前提とする。
1. Xcodeで新規プロジェクトを作成する。
(1) テンプレートとして、Tab Bar Application を選択する。
※ここでは、プロジェクト名を"test"とする。
2. アクション→追加→既存のファイルで、Navigation-based Applicationベースで開発したプロジェクトから、xib,ヘッダファイル(.h)、クラスファイル(.m)を追加する。追加する時は、"デスティネーショングループのフォルダに項目をコピーする(必要な場合)"にチェックを入れる。チェックを入れないと、参照がコピーされるだけとなり、ファイルを更新すると、参照元のファイルも更新されてしまうので注意。
3. 上記1.で作られたMainWindow.xibを更新する。
(1) MainWindow.xlbを InterfaceBuilder で開く。
(2) 階層表示にして、"Tab Bar Controller"をInspectorで開く。
(3) Inspectorの左端のtabでTab Bar Controller/View Controllersの項目の、Titleが"First"の部分を"View Controller"から、"Navigation Controller"に変更する。
(4) 上記(3)操作により、InterfaceBuilder上の"Selected View Controller(First)"が、"Selected Navigation Controller(First)"に自動的に変わるのを確認する。
(5) "Selected Navigation Controller(First)" の2つ下"View Controller(Item)"をクリックし、Inspectorを開く。
(6) Inspectorの左端のtabでViewController/Nib Nameに、NavigationControllerのトップ画面・通常は"RootViewController"を設定する。
(7) Inspectorの右端のtabでClass Identity/Classに、NavigationControllerのトップ画面・通常は"RootViewController"を設定する。
(8)InterfaceBuilderを保存する。
4. Xcodeでソースファイルを更新する。
(1) "testAppDelegate.h"を開いて修正する。
(変更前)
#import <UIKit/UIKit.h>
@interface testAppDelegate : NSObject <UIApplicationDelegate, UITabBarControllerDelegate> {
UIWindow *window;
UITabBarController *tabBarController;
}
@property (nonatomic, retain) IBOutlet UIWindow *window;
@property (nonatomic, retain) IBOutlet UITabBarController *tabBarController;
@end
(変更後)
#import <UIKit/UIKit.h>※テンプレートとして、Tab Bar Applicationを選択した場合は、UITabBarControllerのみ定義されているので、UINavigationControllerを追加記述する。
@interface testAppDelegate : NSObject <UIApplicationDelegate, UITabBarControllerDelegate> {
UIWindow *window;
UITabBarController *tabBarController;
UINavigationController *navigationController;
}
@property (nonatomic, retain) IBOutlet UIWindow *window;
@property (nonatomic, retain) IBOutlet UITabBarController *tabBarController;
@property (nonatomic, retain) IBOutlet UINavigationController *navigationController;
@end
(2) "testAppDelegate.m"を開いて修正する。
(変更前)
#import "testAppDelegate.h"
@implementation testAppDelegate
@synthesize window;
@synthesize tabBarController;
- (void)applicationDidFinishLaunching:(UIApplication *)application {
// Add the tab bar controller's current view as a subview of the window
[window addSubview:tabBarController.view];
}
(変更後)
#import "testAppDelegate.h"
#import "RootViewController.h"
@implementation testAppDelegate
@synthesize window;
@synthesize tabBarController;
@synthesize navigationController;
- (void)applicationDidFinishLaunching:(UIApplication *)application {
// トップレベル(最上位)のRootViewControllerを取得
RootViewController* rootViewController;
rootViewController = (RootViewController*)navigationController.topViewController;
// RootViewControllerを設定(最上位レベルのテーブルのタイトル、セルを設定)
rootViewController.title = @"てすとアプリ"; // ナビゲーション画面のトップ画面のタイトルと、タブボタンのタイトルに反映される。
// ウィンドウの設定と表示
[window addSubview:[navigationController view]];
[window makeKeyAndVisible];
// Add the tab bar controller's current view as a subview of the window
[window addSubview:tabBarController.view];
}
ナビゲーションの表示を下記で実行し、
[window addSubview:[navigationController view]];
[window makeKeyAndVisible];
タブバーの表示を下記で実行する。
[window addSubview:tabBarController.view];
RootViewController.h
#import <UIKit/UIKit.h>
@interface RootViewController : UITableViewController <UINavigationBarDelegate, UITableViewDelegate,
UITableViewDataSource, UITabBarDelegate> {
NSArray* names;
@property (retain) NSArray* names;
@end
■参考記事
http://www.nsdev.co.jp/cgi-bin/wordpress/?p=120
タグ:iPhone