iPhoneアプリの場合、プログラムで、できればObjective-Cでタブビューを作成するにはどうすればよいですか?
2 に答える
UITabBarController を介してUITabBarを作成するのは非常に簡単です。次の例は、AppDelegate クラス内で機能するはずです。
アプリ デリゲート インターフェイス
まず、インターフェイス内でUITabBarControllerを定義します。
UITabBarController *tabBarController;
アプリ デリゲートの実装
次に、実装ファイルのapplication:didFinishLaunchingWithOptions:
メソッド内で、タブ バー コントローラーを初期化します。
// Initialise our tab bar controller
UITabBarController *tabBarController = [[UITabBarController alloc] init];
次に、タブ バー コントローラーに追加するビュー コントローラーを作成する必要があります。タブのタイトル/アイコンを設定するには、これらにいくつかの情報を追加する必要がありますが、それについては最後に説明します。
// Create your various view controllers
UIViewController *testVC = [[TestViewController alloc] init];
UIViewController *otherVC = [[OtherViewController alloc] init];
UIViewController *configVC = [[ConfigViewController alloc] init];
setViewControllers:animated: メソッドにはビュー コントローラーの配列が必要なため、ビュー コントローラーを配列に追加してから解放します。(NSarray がそれらを保持するため。)
// Put them in an array
NSArray *viewControllers = [NSArray arrayWithObjects:testVC, otherVC, configVC, nil];
[testVC release];
[otherVC release];
[configVC release];
次に、UITabBarController にビュー コントローラーの配列を提供し、それをウィンドウに追加します。
// Attach them to the tab bar controller
[tabBarController setViewControllers:viewControllers animated:NO];
// Put the tabBarController's view on the window.
[window addSubview:[tabBarController view]];
最後に、必ずメソッド[tabBarController release];
内で呼び出してくださいdealloc
。
ビューコントローラーの実装
各ビュー コントローラー内で、次のように init メソッド内でタブのタイトルとアイコンを設定することもできます。
// Create our tab bar item
UITabBarItem *tabBarItem = [self tabBarItem];
UIImage *tabBarImage = [UIImage imageNamed:@"YOUR_IMAGE_NAME.png"];
[tabBarItem setImage:tabBarImage];
[tabBarItem setTitle:@"YOUR TITLE"];
これは、プログラムでタブバーを作成する方法です
UINavigationController *BandNavigationController3;
AudienceSettingsViewController *audienceSettingsViewView =[[AudienceSettingsViewController alloc]initWithNibName:@"AudienceSettingsViewController" bundle:nil];
BandNavigationController3 = [[UINavigationController alloc]initWithRootViewController:audienceSettingsViewView];
BandNavigationController3.tabBarItem.title = @"Settings";
BandNavigationController3.tabBarItem.image = [UIImage imageNamed:@"settings.png"];
[BandNavigationController3.tabBarItem initWithTabBarSystemItem:UITabBarSystemItemFavorites tag:4];
BandNavigationController3.navigationBar.hidden = YES;
[bandTabBarArray addObject:BandNavigationController3];
[BandNavigationController3 release];
[audienceSettingsViewView release];
[tabBarController setViewControllers:bandTabBarArray];
[bandTabBarArray release];