ios - 从 AppDelegate 导航时导航栏消失

标签 ios swift uinavigationcontroller uinavigationbar

当用户点击通知托盘时,我需要导航到特定的 View Controller 。因此我需要从 AppDelegate 类重定向它们:

这是 AppDelegate 中的代码:

if let myVc = UIStoryboard(name : "Main",bundle : nil).instantiateViewController(withIdentifier: "MyViewController")
    as? MyViewController {

         myVc.postId = Int(postId)
         if let navigationController = self.window?.rootViewController /* as? UINavigationController */{
          navigationController.show(myVc, sender: Any?.self)
         }
  }

通过使用这段代码,当我点击通知托盘时,我可以重定向到我的 MyViewController,但问题是导航栏不见了,所以应用程序就卡在那里了。

我也尝试过使用它,但是通过下面的代码,它无法重定向到 myPostVc:

if let navigationController = self.window?.rootViewController  as? UINavigationController {
  navigationController.pushViewController(myVc, animated: true)
}

所以我的问题是:

如何从 AppDelegate 导航到特定的 View Controller ,导航栏出现在目标 View Controller 上?

最佳答案

将 tabBar 嵌入到导航 Controller 中(在代码中,当您选择一个 tabbar 并希望将其嵌入到 Storyboard 中的导航 Controller 中时,嵌入是灰色的)并使用它来推送 VC

 if let navigationController = self.window?.rootViewController  as? UINavigationController {
    navigationController.pushViewController(myVc, animated: true)
 }

或者你可以这样做

if let cu = self.window?.rootViewController as? UITabBarController
 {         
       let nav: UINavigationController = UINavigationController()

        self.window?.rootViewController = nav

        let str = UIStoryboard.init(name: "Main", bundle: nil)

        let rr = str.instantiateViewController(withIdentifier: "idOFPushedVC") 

         nav.setViewControllers([cu,rr], animated: true)

  }

关于ios - 从 AppDelegate 导航时导航栏消失,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49223100/

相关文章:

ios - 快速按下按钮时检查单元格是否被选中

arrays - 在正确的地方调用 DispatchGroup()

ios - 以编程方式调用 iOS 上的导航 Controller 后退按钮

ios - 快速 xcode 从导航 Controller 切换到标签栏 Controller 的正确方法

xcode - 无法使用 xcode 6 beta 5 发送发布数据

objective-c - viewDidDisappear 没有在 UINavigationController 上被调用

ios - 如何使用 StoryBoard 在 UITabBarController 中设置选定的选项卡?

ios - 如何让辅助 <View> 使用 PanResponder 进入主视图?

ios - Apple Swift 的类型转换义务

ios - Alamofire 使凭证无效