ios - 如何以编程方式执行 UINavigationController?

标签 ios xcode swift

通常,在 Storyboard中我总是做编辑器 -> 嵌入式导航 Controller

和这段代码一样吗?

myNavigationController = UINavigationController(rootViewController: myController)

但问题在于如何像 Storyboard中一样附加导航 Controller ?

最佳答案

您应该将导航 Controller 设为主 UIWindowrootViewController

如果您完全以编程方式创建界面,在您的应用委托(delegate) application:didFinishLaunchingWithOptions: 中创建主 UIWindow 和主导航 Controller 很常见,然后分配后者到前者的 rootViewController 属性。

class AppDelegate: UIResponder, UIApplicationDelegate {

  var window: UIWindow?
  var navigationController: UINavigationController?

  func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: NSDictionary?) -> Bool {

    self.navigationController = UINavigationController()

    var viewController: UIViewController = UIViewController()
    self.navigationController.pushViewController(viewController, animated: false)

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

    ...more initialisation...

    self.window.makeKeyAndVisible()

    return true
  }
}

编辑:

在评论中回答您的问题:

so is it the same when I create navigation controller from editor -> embedded in navigation controller? I mean like, if I do from editor-> embedded in navigation controller, will it produce the same result as I do programatically?

这取决于你如何看待它。从用户的角度来看,我会说,是的。从程序员的角度来看,我会说不是,因为 Storyboard会生成一堆其他对象(与场景、转场等相关)

换句话说,如果你只关心创建一个导航 View Controller 并附加到你的窗口, Storyboard会像我上面概述的那样做(加上我之前提到的一堆东西并且不会改变“附加”的最终结果,只是这样做的方式)。

如果你的意思是你想重新创建与 Storyboard中创建的设置完全相同的设置,我认为你不能以编程方式创建新的 Storyboard场景,因为 Apple 不会公开所有相关类。

关于ios - 如何以编程方式执行 UINavigationController?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27973332/

相关文章:

ios - 无法使用 Swift 语法过滤器

android - 从设备到智能手机的 BLE 文件传输

ios - 模块 'Builtin' 没有名为 convertUnownedUnsafeToGuaranteed 的成员

swift - Firebase Firestore 时间戳至今问题

ios - 在 Swift 2.0 中控制任务

ios - 将大数除以 1000 并四舍五入到小数点后一位

iphone - Libssh2,使用 xcode 构建库

ios - 如何快速添加iPad和iPhone Assets ?

ios - 如果我的应用程序在其各种 View Controller 中使用位置管理器,我应该在哪里实例化它?

ios - 我可以从崩溃日志中识别越狱手机吗?