ストーリーボードなしで、xcode プロジェクト テンプレートを使用して新しい iPhone タブ付きアプリケーションを作成しました。プロジェクト テンプレートによって生成された FirstViewController と SecondViewController を削除しました。
その後、MyFirstViewController (UITableViewController のサブクラス) と MySecondViewController (UIViewController のサブクラス) という新しいコントローラーを作成し、それらを UINavigationControllers の下に配置しました。
コードを少し変更したので、次のようになりました。
appdelegate.m :
#import "MyFirstViewController.h"
#import "MySecondViewController.h"
....
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
//create view controllers
UITableViewController * vc1 = [[MyFirstViewController alloc] initWithNibName:nil bundle:nil];
UIViewController * vc2 = [[MySecondViewController alloc] initWithNibName:nil bundle:nil];
//create navigation controllers
UINavigationController *nav1 = [[UINavigationController alloc] initWithRootViewController:vc1];
UINavigationController *nav2 = [[UINavigationController alloc] initWithRootViewController:vc2];
//add nav controllers to tab bar controller
self.tabBarController = [[UITabBarController alloc] init];
self.tabBarController.viewControllers = @[nav1, nav2];
self.window.rootViewController = self.tabBarController;
[self.window makeKeyAndVisible];
return YES;
}
tabBarItem が最初のタブに表示されない
MyFirstViewController.m :
- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {
// Setting tabbar item and image here is not showing. It just show empty tab, no title and no image. why o why?
self.tabBarItem.image = [UIImage imageNamed:@"someImage"];
self.tabBarItem.title = @"someName";
}
return self;
}
しかし、2番目のタブはうまく機能しています
MySecondViewController.m :
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// This is showing just fine
self.tabBarItem.image = [UIImage imageNamed:@"someImage"];
self.tabBarItem.title = @"someName";
}
return self;
}
UITableViewController のサブクラスが tabBarItem を表示できない理由は何ですか? その間、 UIViewController はそれをうまく表示できます。