ios – 如何在不使用Swift中的Storyboard的情况下以编程方式加载UIViewController

我有一个名为MyViewController的ViewController,我想以编程方式加载它而不使用stroyboard或xib.

作为后续问题,我想在导航控制器中加载MyTableViewController.同样,我需要以编程方式执行它而不使用Storyboard或xib.

请帮忙.

最佳答案 通过删除Storyboard并在AppDelegate中使用以下代码,我能够解决我的问题

    import UIKit
    import CoreData

    @UIApplicationMain
    class AppDelegate: UIResponder, UIApplicationDelegate {

        var window: UIWindow?
        var navigationController: UINavigationController?

        func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
            // Override point for customization after application launch.
            self.window = UIWindow(frame: UIScreen.mainScreen().bounds)
            // Override point for customization after application launch.
            self.window!.backgroundColor = UIColor.whiteColor()
            self.window!.makeKeyAndVisible()

            let myViewController: MyViewViewController? = MyViewViewController()
            self.navigationController = UINavigationController(rootViewController: myViewController!)
            self.window!.rootViewController = self.navigationController
            return true
        }
 ...
}
点赞