2
在AppDelegate/didFinishLaunchingWithOptions
上創建TabBarController
後,如何在Swift上以編程方式添加導航界面。這裏是我目前的代碼:如何在以編程方式創建標籤欄控制器後添加導航界面(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上正確執行此操作。我期待在AppDelegate
中以單獨的方法設置和修改NavigationBar
。有什麼想法嗎?
感謝您的評論。我不確定我是否按照你的建議。回覆:var navigationController = UINavigationController(rootViewController:viewController));在上面顯示的我的App Delegate設置中,我創建了一個TabBarControllers和2個viewControllers。鑑於我沒有爲該代碼創建viewController標識符,我該如何遵循你的建議? – ckraider
如果您需要每個視圖控制器的導航欄,請使用視圖控制器創建兩個不同的導航控制器作爲根視圖控制器。例如:'var navigationController1 = UINavigationController(rootViewController:purchaseViewController); tabBarController.viewControllers = [navigationController1,navigationController2]' – evnaz
這是有效的。非常感謝。 – ckraider