2009年11月01日

コードでUITabBarControllerを生成する

UITabBarはInterfaceBuilderで定義できるが、コードでも定義することができる。

AppDelegate.h
@interface AppDelegate : NSObject {

UIWindow *window;
UITabBarController *tabBarController;

@property (nonatomic, retain) IBOutlet UITabBarController *tabBarController;

@end


AppDelegate.m
@implementation jalanNaviAppDelegate

@synthesize window;
@synthesize tabBarController;

- (void)applicationDidFinishLaunching:(UIApplication *)application {

self.tabBarController = [[UITabBarController alloc] initWithNibName:nil bundle:nil];
UITabBarController *tab1Controller = [[[UITabBarController alloc] initWithNibName:@"tab1" bundle:nil] autorelease];
UITabBarController *tab2Controller = [[[UITabBarController alloc] initWithNibName:@"tab2" bundle:nil] autorelease];
UITabBarController *tab3Controller = [[[UITabBarController alloc] initWithNibName:@"tab3" bundle:nil] autorelease];
UITabBarController *tab4Controller = [[[UITabBarController alloc] initWithNibName:@"tab4" bundle:nil] autorelease];

tabBarController.viewControllers = [NSArray arrayWithObjects:tab1Controller, tab2Controller, tab3Controller, tab4Controller, nil];


tab1Controller.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"タブ1" image:[UIImage imageNamed:@"hoge1.png"] tag:0];
tab2Controller.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"タブ2" image:[UIImage imageNamed:@"hoge2.png"] tag:1];
tab3Controller.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"タブ3" image:[UIImage imageNamed:@"hoge3.png"] tag:2];
tab4Controller.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"タブ4" image:[UIImage imageNamed:@"hoge4.png"] tag:3];


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

}


- (void)dealloc {

[tabBarController release];
[window release];
[super dealloc];
}


※UITabBarControllerは、UINavigationControllerの下に作らないこと。
UITabBarControllerとUINavigationControllerを共存させる場合は、
UITabBarControllerの下にUINavigationControllerを作る。

■参考記事
Tab Bar ControllerとNavigation Controllerの組み合わせ
 

 
ラベル:iPhone
posted by mobileDeveloper at 22:08 | Comment(2) | TrackBack(0) | UITabBar | このブログの読者になる | 更新情報をチェックする
この記事へのコメント
>UITabBarControllerとUINavigationControllerを共存させる場合は、UITabBarControllerの下にUINavigationControllerを作る。

この場合はdelegateでやるんですか?
TabBarControllerを外出しできるんでしょうか?
Posted by okoba23 at 2010年01月31日 20:39
delegateは必要ありません。
TabBarControllerは外出しできます。
Posted by mobileDeveloper at 2010年02月20日 12:20
コメントを書く
お名前:

メールアドレス:

ホームページアドレス:

コメント:

認証コード: [必須入力]


※画像の中の文字を半角で入力してください。
※ブログオーナーが承認したコメントのみ表示されます。

この記事へのトラックバック