I currently have a CustomTabBarController class and a CustomNavBarController class.
我目前有一个CustomTabBarController类和一个CustomNavBarController类。
The tab bar has five tabs and works fine. The nav bar class adds three buttons (home/messages/profile) to the right side of my navigation bar. When I click the home button, the function below will be triggered.
标签栏有五个标签,工作正常。导航栏类在导航栏的右侧添加了三个按钮(主页/消息/配置文件)。单击主页按钮时,将触发以下功能。
CustomNavBarController.swift (only the tappedHome function)
CustomNavBarController.swift(只有tappedHome函数)
func tappedHome(sender: UIButton) {
// Go to home page (tab bar index 0)
}
This is my AppDelegate and CustomTabBar file.
这是我的AppDelegate和CustomTabBar文件。
AppDelegate.swift
AppDelegate.swift
window?.rootViewController = CustomTabBarController()
let tabBar: UITabBarController = self.window?.rootViewController as! UITabBarController
tabBar.selectedIndex = 0
CustomTabBarController.swift
CustomTabBarController.swift
class CustomTabBarController: UITabBarController, UITabBarControllerDelegate {
override func viewDidLoad() {
super.viewDidLoad()
self.delegate = self
self.viewControllers = [createDummyNavControllerWithTitle("News", imageName: "icon_news", viewController: MainController()),
createDummyNavControllerWithTitle("Games", imageName: "icon_games", viewController: GamesController()),
createDummyNavControllerWithTitle("Students", imageName: "icon_students", viewController: StudentsController()),
createDummyNavControllerWithTitle("Ranking", imageName: "icon_rank", viewController: StatsController()),
createDummyNavControllerWithTitle("Info", imageName: "icon_info", viewController: InfoController())]
}
func tabBarController(tabbarController: UITabBarController, didSelectViewController viewController: UIViewController) {
self.title = viewController.title
}
// Shortcut function for the tab bar items
private func createDummyNavControllerWithTitle(title: String, imageName: String, viewController: UIViewController) -> UINavigationController {
viewController.title = title
let navController = UINavigationController(rootViewController: viewController)
navController.tabBarItem.title = title
navController.tabBarItem.image = UIImage(named: imageName)?.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal)
return navController
}
}
How do I tell my function in CustomNavBarController.swift to load the first index of my tab bar when I click the home button?
当我单击主页按钮时,如何告诉CustomNavBarController.swift中的函数加载我的标签栏的第一个索引?
PS: The reason why I want two ways to actually go back to the home page is because the tab bar won't be visible in every situation.
PS:我之所以想要两种方式来回到主页是因为标签栏在每种情况下都不会显示。
1 个解决方案
#1
2
Executing the following code in your tappedHome(_:)
method should do the trick:
在tappedHome(_ :)方法中执行以下代码应该可以解决问题:
let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
let tabBarController = appDelegate.window!.rootViewController as! UITabBarController
tabBarController.selectedIndex = 0
#1
2
Executing the following code in your tappedHome(_:)
method should do the trick:
在tappedHome(_ :)方法中执行以下代码应该可以解决问题:
let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
let tabBarController = appDelegate.window!.rootViewController as! UITabBarController
tabBarController.selectedIndex = 0