Ios 如何以编程方式将按钮项添加到现有导航栏Xcode
我最近一直致力于以编程方式制作我的应用程序,但我遇到了一个错误,选项卡栏控制器也生成了一个导航控制器(我希望每个页面都有一个导航控制器),但是我正在努力找到一种方法,在栏上添加一个按钮,就像右上角的“完成”按钮一样,来自ViewController本身,而不是选项卡栏控制器 基本上,我正在尝试添加一个提交按钮,该按钮将执行ViewController中声明的操作,但是我发现的唯一方法是在每个视图控制器中单独创建导航控制器,这将降低效率,或者在选项卡栏控制器中添加按钮,但这将意味着复制该功能(特定于“视图控制器”页面上的其他项目)进入选项卡栏控制器,这将是一场噩梦 这是viewDidLoad函数中控制器-newReportScreen上的代码Ios 如何以编程方式将按钮项添加到现有导航栏Xcode,ios,swift,xcode,programmatically,Ios,Swift,Xcode,Programmatically,我最近一直致力于以编程方式制作我的应用程序,但我遇到了一个错误,选项卡栏控制器也生成了一个导航控制器(我希望每个页面都有一个导航控制器),但是我正在努力找到一种方法,在栏上添加一个按钮,就像右上角的“完成”按钮一样,来自ViewController本身,而不是选项卡栏控制器 基本上,我正在尝试添加一个提交按钮,该按钮将执行ViewController中声明的操作,但是我发现的唯一方法是在每个视图控制器中单独创建导航控制器,这将降低效率,或者在选项卡栏控制器中添加按钮,但这将意味着复制该功能(特定
//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)
与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)}
我可能想得太多了,但我一直在努力解决这个问题,因此非常感谢您的帮助!在创建UITabBar时,您已经添加了UINavigationController
viewControllers = tabBarList.map { UINavigationController(rootViewController: $0)}
这是正确的
然后,在每个视图控制器中,可以通过UIViewControllernavigationItem
属性设置其导航栏项:
//setting the nav bar submit button
let submitItem = UIBarButtonItem(barButtonSystemItem: .done, target: nil, action: #selector(SubmitButtonTouched))
self.navigationItem.rightBarButtonItem = submitItem
在viewDidLoad
中执行此操作是正确的