所以我的问题是我正在创建一个没有 Storyboard 的 UITabBarController,并且我对如何创建一个没有 Viewcontroller 的 UITabBar 项目感到困惑。因为我想要做的是我的 UITabBarItem 的第二项是一个不呈现 View Controller 的 Action 。
最佳答案
虽然我使用 Storyboard,但我在应用程序(关闭按钮)中实现了类似的功能。关闭按钮是一个 viewController,但我使用以下代码让它像普通按钮一样工作:
func tabBarController(_ tabBarController: UITabBarController, shouldSelect viewController: UIViewController) -> Bool {
// this provides UI feedback that the button has been pressed, even though it leads to the dismissal
if viewController == self.viewControllers![4] {
viewController.tabBarItem.image? = UIImage(named: "TabBarClose")!.imageWithColor(UIColor.red).withRenderingMode(UIImageRenderingMode.alwaysOriginal)
return false
} else {
return true
}
}
override func viewDidDisappear(_ animated: Bool) {
//sets the close button back to original image
self.viewControllers![4].tabBarItem.image = UIImage(named: "TabBarClose")!
}
func tabBarController(_ tabBarController: UITabBarController, didSelect viewController: UIViewController) {
// this is so it never actually goes to the close buttons viewController
currentTab = self.selectedIndex != 4 ? self.selectedIndex:currentTab
saveTabIndexToPreferences(currentTab!)
}
override func tabBar(_ tabBar: UITabBar, didSelect item: UITabBarItem) {
// assign functionality to the close button
if item.tag == 100 {// this is old code, there is probably a better way to reference the tab
dismissTabBar()
} else {
}
}
编辑(对于被问到的问题)
func selectTabIndexFromPreferences() {
let defaults:UserDefaults = UserDefaults.standard
selectedIndex = defaults.integer(forKey: "songSelectionTabIndex_preference")
}
func saveTabIndexToPreferences(_ index:Int?) {
if index != nil {
let defaults:UserDefaults = UserDefaults.standard
defaults.set(index!, forKey: "songSelectionTabIndex_preference")
}
}
关于ios - 如何在没有链接到 View Controller 的情况下快速创建标签栏项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45799041/