2016-11-06 86 views
2

我在Swift 3.0中創建了一個具有5個選項卡的自定義UITabBarController。我一直在嘗試爲某些選項卡設置不同的導航欄項目,但效果不佳。Swift:爲不同的選項卡設置不同的導航欄按鈕

情況:我把代碼,將改變導航欄按鈕在每個選項卡的viewDidLoad()。

//The customized Tab Bar controller instance. Contained in each of the tabs. 
var mainController: TabBarController? 

override func viewDidLoad() { 
    // ... more code 
    setupNavBar() 
    // ... more code 
} 

func setupNavBar() { 
    // ... more code 
    mainController?.navigationItem.leftBarButtonItem = UIBarButtonItem(image: friendsImage, style: .plain, target: self, action: #selector(handleFindFriends)) 
    // ... more code 
} 

問題:假設標籤#應該有NavBarButton A和標籤#應該有NavBarButton B. 當我從標籤#切換到標籤#2,代碼工作正常; NavBarButton從A更改爲B. 但是,當我單擊Tab#1時,NavBarButton仍然爲B.

如何才能使導航欄按鈕相應更改,即使當我單擊選項卡時以前是?

+1

我們創建一個TabBarController和(NavigationController>視圖控制器)爲每個標籤。所以你可以爲每個ViewController添加barButtonItem。 –

+0

我已將代碼添加到我的答案中,請查看。 –

回答

1

我居然找到了解決辦法XD 在TabBarController:

override func viewWillAppear(_ animated: Bool) { 
    //Original code to set up tabs 

    //Code I added: 
    for i in 0...4 { 
     tabBar.items?[i].tag = i 
    } 
} 
//Code I added: 
override func tabBar(_ tabBar: UITabBar, didSelect item: UITabBarItem) { 
    //Code to run when a certain tab is selected 
} 

不過,非常感謝!

2

我假設您將UITabBarController(或其子類)嵌入到UIViewController中。這是錯誤的,因爲在這種情況下,你傾向於使視圖控制器是通用的,這通常是不好的做法。

相反,我會建議將視圖控制器的層次結構更改爲您在下圖中看到的內容。

enter image description here

UPDATE

如果你在代碼中做到這一點:

@UIApplicationMain 
class AppDelegate: UIResponder, UIApplicationDelegate { 

    var window: UIWindow? 


    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { 

     let tabBarController = UITabBarController() 

     // first tab 
     let firstViewController = UIViewController() 
     _ = firstViewController.view 
     firstViewController.title = "First" 
     let firstNavigationController = UINavigationController(rootViewController: firstViewController) 

     // sets a specific button ("Friends") on a navigation bar for the first screen 
     firstViewController.navigationItem.rightBarButtonItem = UIBarButtonItem(title: "Friends", style: .plain, target: nil, action: nil) 

     // second tab 
     let secondViewController = UIViewController() 
     _ = secondViewController.view 
     secondViewController.title = "Second" 
     let secondNavigationController = UINavigationController(rootViewController: secondViewController) 

     // sets a specific button ("Photos") on a navigation bar for the second screen 
     secondViewController.navigationItem.rightBarButtonItem = UIBarButtonItem(title: "Photos", style: .plain, target: nil, action: nil) 

     // embeds the navigation controllers into the tab bar controller 
     tabBarController.viewControllers = [firstNavigationController, secondNavigationController] 

     // creates a window with the tab bar controller inside 
     window = UIWindow(frame: UIScreen.main.bounds) 
     window?.rootViewController = tabBarController 
     window?.makeKeyAndVisible() 

     return true 
    } 

} 
+0

這就是我的答案,好的工作@Artem Stepanenko – emresancaktar

相關問題