在 AppDelegate 中实例化 UINavigationController

Posted

技术标签:

【中文标题】在 AppDelegate 中实例化 UINavigationController【英文标题】:Instantiate UINavigationController in AppDelegate 【发布时间】:2015-07-18 18:51:53 【问题描述】:

我正在尝试在我的AppDelegate 中用rootViewController 实例化UINavigationController。我看过这个网站,但所有的例子都来自 Objective-C 或使用的故事板(我试图摆脱)。 'HomeScreenController` 是应用程序的根视图。

编辑:这会显示在控制台中

2015-07-18 14:42:25.376 FastFactsSwift[4749:343495] Failed to instantiate the default view controller for UIMainStoryboardFile 'Main' 
- perhaps the designated entry point is not set?

我该如何解决这个问题?


以下代码只导致黑屏:

AppDelegate.swift:

import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate 

var window: UIWindow?


func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool 
    // Override point for customization after application launch.

    self.window?.rootViewController = UINavigationController(rootViewController: HomeScreenController())

    return true

...

HomeScreenController.swift:

import UIKit

class HomeScreenController: UIViewController, UISearchBarDelegate, UITableViewDelegate
    override func viewDidLoad()

        super.viewDidLoad()

        var width = self.view.viewWidth
        var height = self.view.viewHeight

        //Add stuff to the view

...

为什么HomeScreenController 显示为黑屏?

【问题讨论】:

【参考方案1】:

问题在于没有将AppDelegate 配置为不使用情节提要。

How do I create a new Swift project without using Storyboards?

【讨论】:

以上是关于在 AppDelegate 中实例化 UINavigationController的主要内容,如果未能解决你的问题,请参考以下文章