gpt4 book ai didi

ios - Swift:如何通过按导航栏按钮访问选项卡栏项目

转载 作者:搜寻专家 更新时间:2023-11-01 07:21:03 25 4
gpt4 key购买 nike

我目前有一个 CustomTabBarController 类和一个 CustomNavBarController 类。

标签栏有五个标签并且工作正常。导航栏类将三个按钮(主页/消息/个人资料)添加到导航栏的右侧。当我点击主页按钮时,会触发下面的功能。

CustomNavBarController.swift(仅 tappedHome 函数)

func tappedHome(sender: UIButton) {
// Go to home page (tab bar index 0)
}

这是我的 AppDelegate 和 CustomTabBar 文件。

AppDelegate.swift

    window?.rootViewController = CustomTabBarController()
let tabBar: UITabBarController = self.window?.rootViewController as! UITabBarController
tabBar.selectedIndex = 0

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
}
}

当我单击主页按钮时,我如何告诉 CustomNavBarController.swift 中的函数加载我的标签栏的第一个索引?

PS:我之所以想要两种返回主页的方法,是因为标签栏不会在任何情况下都可见。

最佳答案

在您的 tappedHome(_:) 方法中执行以下代码应该可以解决问题:

let appDelegate  = UIApplication.sharedApplication().delegate as! AppDelegate
let tabBarController = appDelegate.window!.rootViewController as! UITabBarController
tabBarController.selectedIndex = 0

关于ios - Swift:如何通过按导航栏按钮访问选项卡栏项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38999367/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com