以编程方式添加TabBarController

6
我想通过编程方式创建一个标签栏控制器和导航控制器。到目前为止,我的代码可以在底部显示一个选项卡栏,但第二个选项卡栏上的OptionViewController按钮没有任何标题。有趣的是,当我点击没有任何东西的按钮时,标题就会出现(并且他的视图也是如此),有人能解释一下我做错了什么吗?我尝试使用以下代码:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{    
    // Override point for customization after application launch.
    [self.window makeKeyAndVisible];

    NSMutableArray *tabItems = [[NSMutableArray alloc] initWithCapacity:2];

    DefaultViewController *dvc = [[DefaultViewController alloc] init];
    UINavigationController *dvc_nc = [[UINavigationController alloc] initWithRootViewController:dvc];
    [tabItems addObject:dvc_nc];
    [dvc release];
    [dvc_nc release];

    OptionsViewController *ovc = [[OptionsViewController alloc] initWithStyle:UITableViewStyleGrouped];
    UINavigationController *ovc_nc = [[UINavigationController alloc] initWithRootViewController:ovc];
    [tabItems addObject:ovc_nc];
    [ovc release];
    [ovc_nc release];

    UITabBarController *tbc = [[UITabBarController alloc] init];
    tbc.viewControllers = tabItems;
    self.tabController = tbc;
    [tabItems release];
    [tbc release];

    [self.window addSubview:self.tabController.view];

    return YES;
}

我认为你需要将UINavigationController作为子视图添加到Tab Bar控制器中,同时将控制类的超类设置为UINavigationController。 - Nathan Fitzgerald - Fitzgenius
问题只是缺少标题,对吧?你在哪里设置了 OptionsViewConbtrollertitle?如果你没有在 init 方法中设置标题,那么 TabBarController 只会从 OptionsVC 中读取一个空标题。我猜你是在 viewDidLoad 等方法中设置了标题属性? - thomas
我猜不是这样的,因为这个代码:[tbc.view addSubview:ovc_nc.view]; 会使整个屏幕变为空白! - Mark
@ Thomas,我确实在ViewDidLoad中设置了标题。奇怪的是,当我点击选项卡时,标题突然出现了! - Mark
3个回答

10

你需要设置UINavigationController的tabBarItem和title而不是它的根视图控制器。

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{    
    // Override point for customization after application launch.
    [self.window makeKeyAndVisible];

    NSMutableArray *tabItems = [[NSMutableArray alloc] initWithCapacity:2];

    DefaultViewController *dvc = [[DefaultViewController alloc] init];
    UINavigationController *dvc_nc = [[UINavigationController alloc] initWithRootViewController:dvc];
    dvc_nc.tabBarItem.title = @"Default";
    dvc_nc.tabBarItem.image = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"Default" ofType:@"png"]];
    [tabItems addObject:dvc_nc];
    [dvc release];
    [dvc_nc release];

    OptionsViewController *ovc = [[OptionsViewController alloc] initWithStyle:UITableViewStyleGrouped];
    UINavigationController *ovc_nc = [[UINavigationController alloc] initWithRootViewController:ovc];
    ovc_nc.tabBarItem.title = @"Option"
    ovc_nc.tabBarItem.image = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"Optiomn" ofType:@"png"]];

    [tabItems addObject:ovc_nc];
    [ovc release];
    [ovc_nc release];

    UITabBarController *tbc = [[UITabBarController alloc] init];
    tbc.viewControllers = tabItems;
    self.tabController = tbc;
    [tabItems release];
    [tbc release];

    [self.window addSubview:self.tabController.view];

    return YES;
}

我已经更新了我的帖子,它将向您展示如何设置tabBarItem。 - rckoenes
现在标题显示正确了,但是现在当我在OptionsView上将标题设置为“Options”,并且使用您的方法将其初始化为“Option”时,当应用程序启动时,我会看到“Default”和“Option”。 当我点击TabBar项时,它会将标题更改为我在viewDidLoad中配置的self.title! - Mark
不要在viewDidLoad中设置标题。 - rckoenes
哈哈,那么我的导航栏就没有标题了!我把它从设置self.title改成了self.navigationItem.title = @"选项";!谢谢。 - Mark

3

我将UITabbarController创建为应用程序的根视图控制器,并使用UINavigationController为UIViewController。

这里再举一个例子:我使用xibs来创建视图控制器。

在AppDelegate.m中,我创建了一个名为setupAppHome的方法。

#pragma mark - SETUP HOME
-(void) setupAppHome{
    NSLog(@"set up the nano home");

    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];

    if (_chatViewController == nil) {
        _chatViewController = [[ChatViewController alloc] initWithNibName:@"ChatViewController" bundle:nil];
        chatNav = [[UINavigationController alloc] initWithRootViewController:_chatViewController];
        chatNav.tabBarItem.title=@"Chat";
        chatNav.tabBarItem.image=[UIImage imageNamed:@"chat_icon.png"];

    }
    if (_callController == nil) {
        _callController = [[CallViewController alloc] initWithNibName:@"CallViewController" bundle:nil];
        callNav = [[UINavigationController alloc] initWithRootViewController:_callController];
        callNav.tabBarItem.title=@"Call";
        callNav.tabBarItem.image=[UIImage imageNamed:@"call_icon.png"];

    }
    if (_contanctsController == nil) {
        _contanctsController = [[ContactsViewController alloc] initWithNibName:@"ContactsViewController" bundle:nil];
        conNav = [[UINavigationController alloc] initWithRootViewController:_contanctsController];
        conNav.tabBarItem.title=@"Contact";
        conNav.tabBarItem.image=[UIImage imageNamed:@"contact_icon.png"];

    }
    if (_settingController == nil) {
        _settingController = [[SettingViewController alloc] initWithNibName:@"SettingViewController" bundle:nil];
        settingNav = [[UINavigationController alloc] initWithRootViewController:_settingController];
        settingNav.tabBarItem.title=@"Setting";
        settingNav.tabBarItem.image=[UIImage imageNamed:@"setting_icon.png"];

    }

    self.tabController = [[UITabBarController alloc] init];

    NSMutableArray          *controllers = [[NSMutableArray alloc] initWithCapacity:4];
    [controllers addObject:chatNav];
    [controllers addObject:callNav];
    [controllers addObject:conNav];
    [controllers addObject:settingNav];


    self.tabController.viewControllers = controllers;//@[chatNav,callNav,conNav,settingNav];

    self.tabController.selectedIndex=0;



    [self.window setRootViewController:self.tabController];
    [self.window makeKeyAndVisible];


}

这段文字是使用iOS 11和Xcode 9编写的。


0
如果有人需要 SWIFT 版本,这个对我有效。感谢 @rckoenes 提供的 objC 答案,我用它将其翻译成了这个版本。
    window?.makeKeyAndVisible()

    let dvc = HomeViewController()
    let dvc_nc = UINavigationController(rootViewController: dvc)
        dvc_nc.tabBarItem.title = "Home"
        dvc_nc.tabBarItem.image = UIImage(named: "HomeIcon")
    controllers.append(dvc_nc)

    let ovc = ProfileViewController()
    let ovc_nc = UINavigationController(rootViewController: ovc)
        ovc_nc.tabBarItem.title = "Profile"
        ovc_nc.tabBarItem.image = UIImage(named: "ProfileIcon")
    controllers.append(ovc_nc)

    let tbc = UITabBarController()
        tbc.viewControllers = controllers

    window?.rootViewController = tbc

    UINavigationBar.appearance().tintColor = UIColor(red: 0.05, green: 0.47, blue: 0.91, alpha: 1.0)
    UINavigationBar.appearance().barTintColor = UIColor(red: 0.05, green: 0.47, blue: 0.91, alpha: 1.0)
    UINavigationBar.appearance().titleTextAttributes = [NSForegroundColorAttributeName: UIColor.whiteColor()]
    UIApplication.sharedApplication().statusBarStyle = UIStatusBarStyle.LightContent

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接