在TabBarController
上创建AppDelegate/didFinishLaunchingWithOptions
后,如何以编程方式在Swift上添加导航界面。以下是我目前的代码:
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
。有什么想法吗?
答案 0 :(得分:2)
要创建导航控制器,请使用:
var navigationController = UINavigationController(rootViewController: viewController));
然后把它放在数组中:
tabBarController.viewControllers = [purchaseViewController, financeViewController, navigationController]
但是,只有在视图控制器加载后才能将UIControl添加到导航栏。在applicationDidFinishLaunching:
上,这是不可能的,因为navigationBar是零。正确的方法是在viewDidLoad()