一尘不染

以编程方式创建选项卡栏控制器后如何添加导航界面(快速)

swift

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

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()

我在看文档。它具有以下准则

- (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上正确执行此操作。我希望在NavigationBar中的单独方法中设置和修改AppDelegate。有什么想法吗?


阅读 212

收藏
2020-07-07

共1个答案

一尘不染

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

var navigationController = UINavigationController(rootViewController: viewController));

然后将其放入数组中:

tabBarController.viewControllers = [purchaseViewController, financeViewController, navigationController]

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

2020-07-07