2016-11-06 89 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

相关问题