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

29 Views Asked by At

I have set up 6 view controllers (serviceVC and facebookVC is in the "more" tab in the TabBar):

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

Then in the createNav function, it calls the handleButtonTap function below (I can call it successfully because "pressed" is printed in all the 6 view controllers) :

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

However, in serviceVC and facebookVC, it fails to push the MenuViewController(). Any idea?

I tied to move the serviceVC and facebookVC to the front ( self.setViewControllers([service,facebookpage,home,infopage,unit,donate], animated: true) ) and then unitVC and donateVC fail to push new view controllers. I guess the problem happens in the "more" tab in the TabBar?

0

There are 0 best solutions below