Interface Builder(Storyboard)を使わずに
TabBarを利用するにはAppDelegate.hとAppDelegate.mにコードを記述します。
▼AppDelegate.h
1 2 3 4 5 6 7 |
@interface AppDelegate : NSObject
{
UIWindow *window;
UITabBarController *tabBarController;
}
@property (nonatomic, retain) UITabBarController *tabBarController;
@end |
▼AppDelegate.m
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
@implementation AppDelegate
@synthesize window;
@synthesize tabBarController;
- (void)applicationDidFinishLaunching:(UIApplication *)application
{
//ここではタブ1〜3それぞれの表示用のViewControllerとして
//First/Second/ThirdViewControllerクラスを指定しています
FirstViewController *firstViewController = [[[FirstViewController alloc] init] autorelease];
SecondViewController *secondViewController = [[[SecondViewController alloc] init] autorelease];
ThirdViewController *thirdViewController = [[[ThirdViewController alloc] init] autorelease];
//tabBarControllerで管理するViewControllerを指定しています。
self.tabBarController = [[UITabBarController alloc] initWithNibName:nil bundle:nil];
tabBarController.viewControllers = [NSArray arrayWithObjects:firstViewController, secondViewController, thirdViewController, nil];
//tabBarControllerをwindowのrootViewControllerに指定します。
window.rootViewController = tabBarController;
[window makeKeyAndVisible];
return YES;
}
- (void)dealloc {
[tabBarController release];
[window release];
[super dealloc];
}
@end |
9〜11行目の個別ページ生成部分と、15行目のTabBar生成部分を変更すれば、タブを減らしたり増やしたりすることができます。
Related posts: