以编程方式创建navigationController(Swift)

MLy*_*yck 46 uinavigationcontroller ios swift

我一直在尝试以编程方式重做我的应用程序上的工作.(不使用故事板)

除了手动制作导航控制器外,我差不多完成了.

我一直在做一些研究,但我找不到任何手动实现这个的文档.(我开始将应用程序作为单一视图应用程序)

目前,我只有1个viewcontroller.当然是appDelegate

导航控制器将在应用程序的所有页面中使用.

如果有人可以帮助我,或发送一些链接到一些适当的文档,以编程方式执行此操作,将不胜感激.

编辑:

我忘了在Swift中提到它.

Jog*_*Com 83

斯威夫特1,2:

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
   self.window = UIWindow(frame: UIScreen.mainScreen().bounds)
   var nav1 = UINavigationController()
   var mainView = ViewController(nibName: nil, bundle: nil) //ViewController = Name of your controller
   nav1.viewControllers = [mainView]
   self.window!.rootViewController = nav1
   self.window?.makeKeyAndVisible()
}
Run Code Online (Sandbox Code Playgroud)

Swift 3+:和Swift 4+

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
   self.window = UIWindow(frame: UIScreen.main.bounds)
   let nav1 = UINavigationController()
   let mainView = ViewController(nibName: nil, bundle: nil) //ViewController = Name of your controller
   nav1.viewControllers = [mainView]
   self.window!.rootViewController = nav1
   self.window?.makeKeyAndVisible()
}
Run Code Online (Sandbox Code Playgroud)


小智 15

我建议用这个骨架开始你的AppDelegate:

1)尽可能使用let!

2)UINavigationController具有rootViewController属性.

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
    let viewController = ViewController(nibName: nil, bundle: nil) //ViewController = Name of your controller
    let navigationController = UINavigationController(rootViewController: viewController)

    self.window = UIWindow(frame: UIScreen.mainScreen().bounds)
    self.window?.rootViewController = navigationController
    self.window?.makeKeyAndVisible()

    return true
}
Run Code Online (Sandbox Code Playgroud)


can*_*ine 11

“AppDelegate”类型的值没有成员“window”

对于那些使用 SceneDelegate.swift 构建新项目的人,您可以使用“var window: UIWindow?” 在 SceneDelegate 中而不是在 AppDelegate 中删除的“var window”

func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
    guard let windowScene = (scene as? UIWindowScene) else { return }

    window?.windowScene = windowScene
    window?.makeKeyAndVisible()

    let viewController = ViewController()
    let navViewController = UINavigationController(rootViewController: viewController)
    window?.rootViewController = navViewController
}
Run Code Online (Sandbox Code Playgroud)


Lin*_*nus 9

这是 SceneDelegate 类的另一个例子:

var window: UIWindow?

func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {

    if let windowScene = scene as? UIWindowScene {

        let window = UIWindow(windowScene: windowScene)    
        let navController = UINavigationController()
        let viewController = ViewController()

        navController.viewControllers = [viewController]            
        window.rootViewController = navController
        self.window = window
        window.makeKeyAndVisible()
    }
}
Run Code Online (Sandbox Code Playgroud)