无法在 TabBarController 的“更多”选项卡中推送视图控制器

Fail to Push View Controller in "More" Tab in TabBarController

提问人:vonnov 提问时间:8/16/2023 最后编辑:vonnov 更新时间:8/16/2023 访问量:27

问:

我已经设置了 6 个视图控制器(位于 TabBar 的“更多”选项卡中):serviceVCfacebookVC

@available(iOS 13.0, *)
private func setupTabs(){
    let home = self.createNav(with: "home", and: UIImage(systemName: "house"), vc: HomeVC())
    let unit = self.createNav(with: "unit", and: UIImage(systemName: "mappin.and.ellipse"), vc: UnitVC())  
    let donate = self.createNav(with: "donate", and: UIImage(systemName: "heart.circle"), vc: DonateVC())
    let infopage = self.createNav(with: "About us", and: UIImage(systemName: "newspaper"), vc: InfoVC())
    let facebookpage = self.createNav(with: "Facebook", and: UIImage(named: "facebookIcon.png"), vc: FacebookVC())
    let service = self.createNav(with: "service", and: UIImage(systemName: "info.circle"), vc: ServiceVC())
    self.setViewControllers([home,infopage,unit,donate,service,facebookpage], animated: true)
}

然后在函数中,它调用以下函数(我可以成功调用它,因为所有 6 个视图控制器中都打印了“pressed”):createNavhandleButtonTap

@available(iOS 13.0, *)
private func createNav(with title: String, and image: UIImage?, vc: UIViewController) -> UINavigationController{
    let nav = UINavigationController(rootViewController: vc)
    
    
    nav.tabBarItem.title = title
    nav.tabBarItem.image = image
    
    nav.viewControllers.first?.navigationItem.title = ""
    
    nav.viewControllers.first?.navigationItem.rightBarButtonItem = UIBarButtonItem(image: UIImage(systemName: "list.bullet"), style: .plain, target: self, action: #selector(handleButtonTap))
    return nav
}

@available(iOS 13.0, *)
@objc private func handleButtonTap(){
    print("pressed")
    guard let selectedNavController = self.selectedViewController as? UINavigationController else{return}
    
    let vc = MenuViewController()
    selectedNavController.pushViewController(vc, animated: true)
}

但是,在 和 中,它无法推送 .有什么想法吗?serviceVCfacebookVCMenuViewController()

我绑住了将 和 移动到前面 ( ),然后无法推送新的视图控制器。我猜问题发生在 TabBar 的“更多”选项卡中?serviceVCfacebookVCself.setViewControllers([service,facebookpage,home,infopage,unit,donate], animated: true)unitVCdonateVC

iOS Swift UITabbarController

评论

0赞 HangarRash 8/16/2023
您需要发布完整功能。否则,我们不知道正在设置什么。createNav
0赞 HangarRash 8/16/2023
您是否验证了“更多”选项卡中视图控制器的 in 是否返回?在 后面添加一个 并查看。guardhandleButtonTapprintguard
0赞 vonnov 8/16/2023
我验证了selectedNavController是否返回一个值,以下是打印的值: 按下 <UINavigationController: 0x104867600>
0赞 vonnov 8/16/2023
很抱歉错过了 createNav 函数,我刚刚编辑了问题。
0赞 HangarRash 8/16/2023
尝试替换为 并替换为 .这可能无济于事,但肯定是更简单的方法。nav.viewControllers.first?.navigationItem.title = ""vc.navigationItem.title = ""nav.viewControllers.first?.navigationItem.rightBarButtonItem = UIBarButtonItem...vc.navigationItem.rightBarButtonItem = UIBarButtonItem...

答: 暂无答案