gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 12:14:09 26 4
gpt4 key购买 nike

我创建了一个带有 5 个按钮的 CustomTabBarController 类,可以完美地工作。

每个按钮都会触发一个单独的 View Controller 。

示例:home按钮触发homeViewController并将其完美地显示在屏幕上。

homeViewController 中,我使用以下代码创建了一个右栏按钮项:

let homeButton = UIBarButtonItem(image: UIImage(named: "HomeButton"), style: .plain, target: self, action:#selector(homeViewController.goToHomeVC)

self.navigationItem.rightBarButtonItem = homeButton

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

如何实现我的 tappedHome 函数,以便它可以访问选项卡栏项目(Home),从而触发 homeViewController 并将其显示在屏幕上?

最佳答案

基本上,这不是实际的UITabBarController,因此,您必须手动维护推送和弹出。

这里的推送和弹出代码将帮助您在五个 View Controller 类之间移动。

func pushIfRequired(className:AnyClass) {

if (UIViewController.self != className) {
print("Your pushed class must be child of UIViewController")
return
}
let storyboard : UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
var isPopDone = false
let mainNavigation = UIApplication.shared.delegate?.window??.rootViewController as? UINavigationController
let viewControllers = mainNavigation!.viewControllers
for vc in viewControllers {
if (type(of: vc) == className) {
mainNavigation?.popToViewController(vc, animated: true)
isPopDone = true
break
}
}
if isPopDone == false{
let instanceSignUp = storyboard.instantiateViewController(withIdentifier: NSStringFromClass(className)) // Identifier must be same name as class
mainNavigation?.pushViewController(instanceSignUp, animated: true)
}
}

用途

pushIfRequired(className: FirstTabVC.self)

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

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