How to load UIViewController programmatically without using storyboards in Swift

I have a ViewController called MyViewController and I want to load it programmatically without a streetboard or xib.

As a subsequent scroll, I would like to load MyTableViewController inside the navigation controller. Again, I need to do this programmatically without using Storyboard or xib.

Please, help.

+4
source share
1 answer

I managed to solve my problem by removing Storyboard and using the following code in 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
        }
 ...
}
+1
source

Source: https://habr.com/ru/post/1609115/


All Articles