来自 ViewController 的导航控制器

UINavigation Controller from a ViewController

我正在尝试从主页ViewController 推送第二个ViewController 作为导航,但还没有导航。然而,在我按下 SecondViewController 之后,发生了转换,但没有显示导航栏。从 ViewController 执行导航转换的最佳方式是什么?

我对 AppDelegate 所做的(我对 appdelegate 所做的是因为我正在处理 pushNotifications 响应)是验证当前控制器是否有导航:

extension UIApplication {
    
    class func getTopViewController(base: UIViewController? = UIApplication.shared.keyWindow?.rootViewController) -> UIViewController? {
        
        if let nav = base as? UINavigationController {
            return getTopViewController(base: nav.visibleViewController)
            
        } else if let tab = base as? UITabBarController, let selected = tab.selectedViewController {
            return getTopViewController(base: selected)
            
        } else if let presented = base?.presentedViewController {
            return getTopViewController(base: presented)
        }
        return base
    }
}

因此,如果导航可用,我 return 然后调用另一个函数:

if let _topVC = UIApplication.getTopViewController(), UserHelper.shared.user.cpf != nil{
            _topVC.didAcceptPushNotification()
        }

因为 ViewController 有一个导航,所以我创建了一个 ViewController 的扩展来设置导航向前:

extension UIViewController{
    @objc func didAcceptPushNotification() {
        DeepLinkManager.shared.checkDeepLink(navigationController: navigationController)
    }
}

那我就说:

let notificationCenterVC = NotificationCentralListViewController(user: UserHelper.shared.user, notificationSpotlightId: nil)
        self.navigationController?.pushViewController(notificationCenterVC, sender: self)

但是 VC 转换后没有显示导航栏。我做错了什么?

终于找到错误了。基本上我叫 self.navigationController?.navigationBar.isHidden = false 在推送 SecondViewController 之前,导航出现了:

private func goToCentralDeNotificacoes() {
        let notificationCenterVC = NotificationCentralListViewController(user: UserHelper.shared.user, notificationSpotlightId: nil)
        self.navigationController?.navigationBar.isHidden = false
        self.navigationController?.pushViewController(notificationCenterVC, animated: true)
    }