如何以编程方式创建标签栏控制器后添加导航界面(Swift) [英] How to add navigation interface after create a tab bar controller programmatically (Swift)

查看:166
本文介绍了如何以编程方式创建标签栏控制器后添加导航界面(Swift)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

AppDelegate / didFinishLaunchingWithOptions上创建 TabBarController 之后,如何在Swift上以编程方式添加导航界面。以下是我当前的代码:

How do I add a navigation interface programmatically on Swift after I created a TabBarController on AppDelegate/didFinishLaunchingWithOptions. Here are my current codes:

self.window = UIWindow(frame: UIScreen.mainScreen().bounds)


    let tabBarController = UITabBarController()
    let purchaseViewController = PurchaseViewController(nibName: "PurchaseViewController", bundle: nil)
    let financeViewController = FinanceViewController(nibName: "FinanceViewController", bundle: nil)



    tabBarController.viewControllers = [purchaseViewController, financeViewController]

    self.window!.rootViewController = tabBarController
    self.window!.makeKeyAndVisible()

我正在查看文档。它有以下指导原则

I'm looking at the documentation. It has the following guidelines

- (void)applicationDidFinishLaunching:(UIApplication *)application {
self.tabBarController = [[UITabBarController alloc] init];

MyViewController1* vc1 = [[MyViewController1 alloc] init];
MyViewController2* vc2 = [[MyViewController2 alloc] init];
MyViewController3* vc3 = [[MyViewController3 alloc] init];
MyNavRootViewController* vc4 = [[MyNavRootViewController alloc] init];
UINavigationController* navController = [[UINavigationController alloc]
                        initWithRootViewController:vc4];

NSArray* controllers = [NSArray arrayWithObjects:vc1, vc2, vc3, navController, nil];
tabBarController.viewControllers = controllers;

window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
window.rootViewController = tabBarController;
[window makeKeyAndVisible];
}

问题,我无法弄清楚如何在Swift上正确地做到这一点。我想在 AppDelegate 中的单独方法中设置和修改 NavigationBar 。有什么想法?

Problem, I can't figure out how to do this properly on Swift. I'm looking to setup and modify the NavigationBar in a separate method in the AppDelegate. Any thoughts?

推荐答案

要创建导航控制器,请使用:

To create a navigation controller use:

var navigationController = UINavigationController(rootViewController: viewController));

然后把它放在数组中:

tabBarController.viewControllers = [purchaseViewController, financeViewController, navigationController]

但是,只有在视图控制器加载后才能将UIControl添加到导航栏。在 applicationDidFinishLaunching:这是不可能的,因为navigationBar是零。正确的方法是在viewDidLoad()

But, you can to add UIControls to navigation bar only after view controller did load. On applicationDidFinishLaunching: it's impossible, because the navigationBar is nil. Proper way is to add controls to navigationBar in the viewDidLoad()

这篇关于如何以编程方式创建标签栏控制器后添加导航界面(Swift)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆