简体   繁体   中英

How to add button item to existing navigation bar programmatically Xcode

I have recently been working on making my application programmatically and I have run into an error, where the tab bar controller is producing a navigation controller as well (which I want for every page), however I am struggling to find a way to add a button onto the bar, like a done button in the top right, from the ViewController itself - not the tab bar controller.

Basically I am trying to add a submit button which will perform an action as declared in the ViewController, however the only way I have found is to either make the navigation controller individually in each View controller which would be less efficient or to add the button in the Tab Bar controller, but that would mean copying the function (which is specific to other items on the View controller page) into the tab bar controller which would be a nightmare.

This is the code on the Controller - newReportScreen in the viewDidLoad function

        //setting the nav bar submit button
        let navBar = UINavigationBar()
        view.addSubview(navBar)
        let submitItem = UIBarButtonItem(barButtonSystemItem: .done, target: nil, action: #selector(SubmitButtonTouched))
        let navItem = UINavigationItem(title: "Submit")
        navItem.rightBarButtonItem = submitItem
        navBar.setItems([navItem], animated: false)

which is conflicting with the code in the TabBar controller


        //setting up the variable for the first view controller which will be used for the tab bar
        let firstViewController = MapVC()
        //set the nav title
        firstViewController.title = "Home"
        //initialising the first tab bar item, which will have the title of Home, the image named below and the tag number, showing the position on the bar
        firstViewController.tabBarItem = UITabBarItem(title: "Home", image: UIImage(named: "Home.png"), tag: 0)
        //initialising the second of the view controllers which will be used to access from the tab bar controller
        let secondViewController = localReportsTVC()
        //set the nav title
        secondViewController.title = "Local Reports"
        //setting the second view controller on the tab bar and giving it a title, image and location on the bar
        secondViewController.tabBarItem = UITabBarItem(title: "Local Reports", image: UIImage(named: "Local.png"), tag: 1)
        //setting up the third view controller to be referenced on the tab bar controller
        let thirdVC = NewReportScreen()
        //set the nav title
        thirdVC.title = "New Report"
        //setting the third view conteroller to be on the tab bar with the image, name and the location on the bar in relation to the other items
        thirdVC.tabBarItem = UITabBarItem(title: "New Report", image: UIImage(named: "Plus Icon.png"), tag: 2)
        //setting up the third view controller to be referenced in the tab bar controller
        let fourthVC = MyReportsTVC()
        //set the nav title
        fourthVC.title = "My Reports"
        //setting the third item on the tab bar up so that it has a position, image and title
        fourthVC.tabBarItem = UITabBarItem(title: "My Reports", image: UIImage(named: "MyReports.png"), tag: 3)
        //setting up the fifth section of the tab bar, where it will be referenced later
        let fithVC = SettingsScreen()
        //set the nav title
        fithVC.title = "Settings"
        //setting up the fifth item, so that it has a title, image and position on the bar
        fithVC.tabBarItem = UITabBarItem(title: "Settings", image: UIImage(named: "Settings Icon.png"), tag: 4)
        //initialising the final tab bar wih all of the elements from above
        let tabBarList = [firstViewController, secondViewController, thirdVC, fourthVC, fithVC]
        //setting the view controllers equal to the tab bar list defined above - also adding in the navigation controller to each of the tabs so that they have a title and also a navigation controller to add the back button in
        viewControllers = tabBarList.map { UINavigationController(rootViewController: $0)}

I may be overthinking this, but I have struggled to work around this, so any help is greatly appreciated!

When creating your UITabBar you're already adding UINavigationController's.

viewControllers = tabBarList.map { UINavigationController(rootViewController: $0)}

This is correct.

Then in each View Controller, you can set its navigation bar items via UIViewControllers navigationItem property:

//setting the nav bar submit button
let submitItem = UIBarButtonItem(barButtonSystemItem: .done, target: nil, action: #selector(SubmitButtonTouched))
self.navigationItem.rightBarButtonItem = submitItem

doing this in viewDidLoad is correct.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM