来自 ViewController 的 UINavigation 控制器

UINavigation Controller from a ViewController

提问人:Bruno Vavretchek 提问时间:5/31/2021 最后编辑:Bruno Vavretchek 更新时间:5/31/2021 访问量:44

问:

我试图在没有导航的情况下从 HomeViewController 将 SecondViewController 推送为 Navigation。但是,在我按下 SecondViewController 后,发生了转换,但没有显示导航栏。从 ViewController 执行作为 Navigation 转换的最佳方式是什么?

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

因此,如果 Navigation 可用,我会返回它,然后调用另一个函数:

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 转换后没有显示导航栏。我做错了什么?

Swift iPhone UI导航控制器

评论


答:

0赞 Bruno Vavretchek 5/31/2021 #1

最后,我弄清楚了错误。基本上我在推送 SecondViewController 之前调用了,然后出现了 Navigation:self.navigationController?.navigationBar.isHidden = false

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