我刚刚使用 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;
}
但第二个标签工作得很好
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 可以很好地显示它。