[英]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.我最近一直致力于以编程方式制作我的应用程序,但遇到了一个错误,其中标签栏控制器也在生成一个导航控制器(我想要每个页面),但是我正在努力寻找一种方法来添加一个按钮到栏上,就像右上角的完成按钮一样,来自 ViewController 本身 - 而不是标签栏控制器。
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.基本上我试图添加一个提交按钮,它将执行在 ViewController 中声明的操作,但是我发现的唯一方法是在每个视图控制器中单独制作导航控制器,这会降低效率或添加按钮标签栏控制器,但这意味着将功能(特定于视图控制器页面上的其他项目)复制到标签栏控制器中,这将是一场噩梦。
This is the code on the Controller - newReportScreen in the viewDidLoad function这是控制器上的代码 - viewDidLoad 函数中的 newReportScreen
//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与 TabBar 控制器中的代码冲突
//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.创建 UITabBar 时,您已经添加了 UINavigationController。
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:然后在每个视图控制器中,您可以通过 UIViewControllers
navigationItem
属性设置其导航栏项:
//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.在
viewDidLoad
这样做是正确的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.