2014-12-21 29 views
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。有什麼想法嗎?

回答

1

要創建一個導航控制器使用:

var navigationController = UINavigationController(rootViewController: viewController)); 

然後,只需把它放在數組中:

tabBarController.viewControllers = [purchaseViewController, financeViewController, navigationController] 

但是,你可以到UIControls添加到導航欄只有在視圖控制器做負載。在applicationDidFinishLaunching:這是不可能的,因爲navigationBar是零。正確的方法是將控件添加到viewDidLoad中的navigationBar()

+0

感謝您的評論。我不確定我是否按照你的建議。回覆:var navigationController = UINavigationController(rootViewController:viewController));在上面顯示的我的App Delegate設置中,我創建了一個TabBarControllers和2個viewControllers。鑑於我沒有爲該代碼創建viewController標識符,我該如何遵循你的建議? – ckraider

+1

如果您需要每個視圖控制器的導航欄,請使用視圖控制器創建兩個不同的導航控制器作爲根視圖控制器。例如:'var navigationController1 = UINavigationController(rootViewController:purchaseViewController); tabBarController.viewControllers = [navigationController1,navigationController2]' – evnaz

+0

這是有效的。非常感謝。 – ckraider

相關問題