以编程方式创建一个 navigationController (Swift) [英] Creating a navigationController programmatically (Swift)

查看:24
本文介绍了以编程方式创建一个 navigationController (Swift)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试以编程方式重做我的应用程序上的工作.(不使用情节提要)

I've been trying to redo the work on my app programmatically. (Without the use of storyboards)

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

I'm almost done, except making the navigation controller manually.

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

I've been doing some research but I can't find any documentation of implementing this manually. (I started making the app as a Single View Application)

目前,我只有 1 个视图控制器.当然还有 appDelegate

Currently, I only have 1 viewcontroller. And of course the appDelegate

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

The navigation Controller, will be used throughout all pages in the application.

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

If anyone could help me out, or send a link to some proper documentation for doing this programmatically it would be greatly appreciated.

我忘了提到它是在 Swift 中的.

I forgot to mention it's in Swift.

推荐答案

Swift 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()
}

Swift 4+:和 Swift 5+

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()
}

这篇关于以编程方式创建一个 navigationController (Swift)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆