nwpL
nwpL

Reputation: 39

How to show a ViewController in your iOS app by only using Code?

I want to show a ViewController in my app without using a storyboard entry point.

I made sure that the default storyboard entry point is deleted and that the storyboard id and name is correct. Still everything I get is just a black screen when running the app on the simulator.

Here is my AppDelegate.swift file.

import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        // Override point for customization after application launch.


        window = UIWindow(frame: UIScreen.main.bounds)

        let sb = UIStoryboard(name: "Main", bundle: nil)
        let initialViewController = sb.instantiateViewController(withIdentifier: "VC")


        window?.rootViewController = initialViewController
        window?.makeKeyAndVisible()




        return true
    }

    // MARK: UISceneSession Lifecycle

    func application(_ application: UIApplication, configurationForConnecting connectingSceneSession: UISceneSession, options: UIScene.ConnectionOptions) -> UISceneConfiguration {
        // Called when a new scene session is being created.
        // Use this method to select a configuration to create the new scene with.
        return UISceneConfiguration(name: "Default Configuration", sessionRole: connectingSceneSession.role)
    }

    func application(_ application: UIApplication, didDiscardSceneSessions sceneSessions: Set<UISceneSession>) {
        // Called when the user discards a scene session.
        // If any sessions were discarded while the application was not running, this will be called shortly after application:didFinishLaunchingWithOptions.
        // Use this method to release any resources that were specific to the discarded scenes, as they will not return.
    }


}

Every kind of help is appreciated.

Upvotes: 0

Views: 545

Answers (2)

Dilan
Dilan

Reputation: 2688

If you are using Viewcontrollers and xcode 11.3.1 or later, follow these steps after create new project.

  1. Remove Application Scene Manifest from info.plist info.plist
  2. Delete SceneDelegate.swift file

  3. Remove Swift UI related methods from AppDelegate file.

  4. Delete ContentView.swift file

  5. Create new ViewController file File>New>Cocoa Touch Class create new view controller

  6. set our MainViewController at AppDelegate file.

Add this

window = UIWindow(frame: UIScreen.main.bounds)
   window!.rootViewController = MainViewController()
   window!.makeKeyAndVisible()
   window?.rootViewController = UINavigationController(rootViewController: MainViewController())

Upvotes: 0

Rob C
Rob C

Reputation: 5073

It's a little bit different with the SceneDelegate that with the application delegate. You use a UIWindowScene to construct your UIWindow.

    let sb = UIStoryboard(name: "Main", bundle: nil)
    let initialViewController = sb.instantiateViewController(withIdentifier: "VC")

    if let windowScene = scene as? UIWindowScene {
        let window = UIWindow(windowScene: windowScene)
        window.rootViewController = initialViewController
        self.window = window
        window.makeKeyAndVisible()
    }

Upvotes: 0

Related Questions