Programmatically set the initial view controller using Storyboards

前端 未结 22 1917
感情败类
感情败类 2020-11-22 15:00

How do I programmatically set the InitialViewController for a Storyboard? I want to open my storyboard to a different view depending on some condition which may

相关标签:
22条回答
  • 2020-11-22 15:23

    Swift 5 or above# make route view controller by this simple code. If you are using xcode 11 or above first initialise var window: UIWindow? in AppDelegate

    let rootVC = mainStoryboard.instantiateViewController(withIdentifier: "YOURCONTROLLER") as! YOURCONTROLLER
    
            navigationController.setNavigationBarHidden(true, animated: true)
            UIApplication.shared.windows.first?.rootViewController = UINavigationController.init(rootViewController: rootVC)
            UIApplication.shared.windows.first?.makeKeyAndVisible()
    
    0 讨论(0)
  • 2020-11-22 15:27

    How to without a dummy initial view controller

    Ensure all initial view controllers have a Storyboard ID.

    In the storyboard, uncheck the "Is initial View Controller" attribute from the first view controller.

    If you run your app at this point you'll read:

    Failed to instantiate the default view controller for UIMainStoryboardFile 'MainStoryboard' - perhaps the designated entry point is not set?
    

    And you'll notice that your window property in the app delegate is now nil.

    In the app's setting, go to your target and the Info tab. There clear the value of Main storyboard file base name. On the General tab, clear the value for Main Interface. This will remove the warning.

    Create the window and desired initial view controller in the app delegate's application:didFinishLaunchingWithOptions: method:

    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
    {
        self.window = [[UIWindow alloc] initWithFrame:UIScreen.mainScreen.bounds];
    
        UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
    
        UIViewController *viewController = // determine the initial view controller here and instantiate it with [storyboard instantiateViewControllerWithIdentifier:<storyboard id>];
    
        self.window.rootViewController = viewController;
        [self.window makeKeyAndVisible];
    
        return YES;
    }
    
    0 讨论(0)
  • 2020-11-22 15:27

    SWIFT 5

    If you don't have a ViewController set as the initial ViewController in storyboard, you need to do 2 things:

    1. Go to your project TARGETS, select your project -> General -> Clear the Main Interface field.
    2. Always inside project TARGETS, now go to Info -> Application Scene Manifest -> Scene Configuration -> Application Session Role -> Item0 (Default Configuration) -> delete the storyboard name field.

    Finally, you can now add your code in SceneDelegate:

    func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
        // Use this method to optionally configure and attach the UIWindow `window` to the provided UIWindowScene `scene`.
        // If using a storyboard, the `window` property will automatically be initialized and attached to the scene.
        // This delegate does not imply the connecting scene or session are new (see `application:configurationForConnectingSceneSession` instead).
        guard let windowScene = (scene as? UIWindowScene) else { return }
    
        window = UIWindow(windowScene: windowScene)
    
    
        let storyboard = UIStoryboard(name: "Main", bundle: nil)
        // Make sure you set an Storyboard ID for the view controller you want to instantiate
        window?.rootViewController = storyboard.instantiateViewController(withIdentifier: identifier)
        window?.makeKeyAndVisible()
    
    }
    
    0 讨论(0)
  • 2020-11-22 15:28

    You can set initial view controller using Interface Builder as well as programmatically.

    Below is approach used for programmatically.

    Objective-C :

            self.window = [[UIWindow alloc] initWithFrame:UIScreen.mainScreen.bounds];
            UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
    
            UIViewController *viewController = [storyboard instantiateViewControllerWithIdentifier:@"HomeViewController"]; // <storyboard id>
    
            self.window.rootViewController = viewController;
            [self.window makeKeyAndVisible];
    
            return YES;
    

    Swift :

            self.window = UIWindow(frame: UIScreen.mainScreen().bounds)
            let mainStoryboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
    
            var objMainViewController: MainViewController = mainStoryboard.instantiateViewControllerWithIdentifier("MainController") as! MainViewController
    
            self.window?.rootViewController = objMainViewController
    
            self.window?.makeKeyAndVisible()
    
            return true
    
    0 讨论(0)
提交回复
热议问题