Appdelegate中的UiNavigation控制器

时间:2019-06-23 07:08:06

标签: ios swift uinavigationcontroller appdelegate

我希望我的导航控制器在整个应用程序中都可用。是否可以在我的应用程序委托中创建UI导航控制器对象,并使它在整个应用程序中可用?我可以使用此对象在整个应用程序中使用导航吗?

我在Swift的app-delegate中创建了一个对象。但效果不理想。在iPhone模拟器中,仅显示导航栏,除了查看导航栏外我什么也不能做。背景完全变黑了。

3 个答案:

答案 0 :(得分:0)

NavigationViewController需要一个rootViewController,我想您是在AppDelegate中设置代码,例如show:

window?.rootViewController = UINavigationController(rootViewController: UIViewController())

要解决此问题,rootViewController应该是特定的ViewController而不是虚拟ViewController。 您应在ViewController内创建一个FirstViewController之类的AppDelegate

window?.rootViewController = UINavigationController(rootViewController: FirstViewController())

答案 1 :(得分:0)

didFinishLaunchingWithOptions 函数的 AppDelegate 中,您可以添加以下代码行

     let storyBoard = UIStoryboard(name: "Main", bundle: nil)
     let firstViewController = storyBoard.instantiateViewController(withIdentifier: "FirstViewController")
     let navController = UINavigationController()
     navController.navigationBar.isHidden = true
     window = UIWindow(frame: UIScreen.main.bounds)
     window?.rootViewController = navController(rootViewController: firstViewController)                             
     window?.makeKeyAndVisible()

答案 2 :(得分:0)

我不确定其他答案是否足够,所以我将投入2美分:

在AppDelegate文件中,您可以根据建议创建对UINavigationController的引用

    @UIApplicationMain
    class AppDelegate {
         var window: UIWindow?
         var firstViewController: FirstViewController?
         var baseNavController: UINavigationController?
         ...
         func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
              firstViewController = FirstViewController(nibName: "FirstViewController", bundle: nil)
              guard let first = firstViewController else { return //you gotta handle this somehow }
              baseNavController = UINavigationController(rootViewController: first)
              window = UIWindow(frame: UIScreen.main.bounds)
              window?.rootViewController = baseNavController
              window?.makeKeyAndVisible()
         }
    }

此时,您可以像这样在项目中的任何位置访问baseNavController

if let delegate = UIApplication.shared.delegate as? AppDelegate {
    print(delegate.baseNavController)
}