Configuring the Programmatically Swift Initial View Controller

I saw Objective-C long answers to this here when the stack overflows, but not quick answers.

How can I change the source view controller programmatically in swift, from a view controller?

I would think it would look something like this:

let storyboard = UIStoryboard(name: "Main", bundle: nil) storyboard.setInitialViewController(identifier: ViewController()) 

But no, it does nothing. The first line is good, but the functions of the second line simply do not exist.

+7
ios swift storyboard
source share
3 answers

To do this in the view controller and not in the application’s deletion: Just select the link to the AppDelegate in the view controller and reset its window object with the correct view controller, since it is rootviewController.

Step 1 Make some NSUserDefault that the user can configure. A few buttons, some switches in the form of a table, something. Then, when the user clicks the button, we change the NSUserDefault.

 @IBAction func SwitchLaunchViewtoViewController2(sender: AnyObject) { defaults.setObject("ViewController2", forKey: "LaunchView") } @IBAction func SwitchLaunchViewtoViewController1(sender: AnyObject) { defaults.setObject("ViewController1", forKey: "LaunchView") } 

Connect a couple of buttons in the settings view controller to these functions, and we started.

Step 2 Configure the storyboard IDs for all the storyboards that you want to set as the launch view. Thus, for each view controller, which may be the original view controller:

-Wed into your storyboard.

-Click on the view controller.

- In the sidebar on the right, click the icon that looks like the newspaper you are managing the class on.

- In the "Identification" section (third line), select the "Use Storyboard ID" check box (make sure it is turned on), and then type something like "VC1" in the "Storyboard" text box. Make sure you select a different storyboard identifier for each view controller.

-Repeat for each view controller.

Step 3 Configure your initial view controller in the AppDelegate.swift file. Go to the func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool section func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool your application delegate.

Add this for reading from NSUserDefault created earlier:

 let defaults = NSUserDefaults.standardUserDefaults() if let launchview = defaults.stringForKey("LaunchView") { } 

This looks for an NSUserDefault line called "LaunchView" (which you created in step 1) and installs it in a new version of the launch launcher if it finds the corresponding NSUserDefault.

Then, in brackets if let launchview... , we want to check what you installed LaunchView . For each object that you set for LaunchView in step 1 (in the example I made "ViewController2" and "ViewController1" ), you should check it here. So, inside these brackets we add the following:

 if launchview == "ViewController2" { } else if launchview == "ViewController1" { } 

Then, inside each of these if statements, we add the following code:

 let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate appDelegate.window = UIWindow(frame: UIScreen.mainScreen().bounds) let mainStoryboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil) // this assumes your storyboard is titled "Main.storyboard" let yourVC = mainStoryboard.instantiateViewControllerWithIdentifier("YOUR_VC_IDENTIFIER") as! YourViewController // inside "YOUR_VC_IDENTIFIER" substitute the Storyboard ID you created in step 2 for the view controller you want to open here. And substitute YourViewController with the name of your view controller, like, for example, ViewController2. appDelegate.window?.rootViewController = yourVC appDelegate.window?.makeKeyAndVisible() 

This will open the selected window when your application finishes downloading after it has been in the background for a while.

Your finished didFinishLoadingWithOptions section of your AppDelegate might look something like this: (don't just copy and paste, read the instructions above)

 func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool { let defaults = NSUserDefaults.standardUserDefaults() if let launchview = defaults.stringForKey("LaunchView") { if launchview == "ViewController1" { let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate appDelegate.window = UIWindow(frame: UIScreen.mainScreen().bounds) let mainStoryboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil) let yourVC = mainStoryboard.instantiateViewControllerWithIdentifier("VC1") as! ViewController1 appDelegate.window?.rootViewController = yourVC appDelegate.window?.makeKeyAndVisible() } else if launchview == "ViewController2" { let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate appDelegate.window = UIWindow(frame: UIScreen.mainScreen().bounds) let mainStoryboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil) let yourVC = mainStoryboard.instantiateViewControllerWithIdentifier("VC1") as! ViewController1 appDelegate.window?.rootViewController = yourVC appDelegate.window?.makeKeyAndVisible() } } return true } 

Hope this helps you, and many thanks to Ankit Goel, who helped me with this so much. Read more below.

One final note : if you use the switches in the settings view, make sure that in the viewDidLoad of this settings view controller you are reading from the NSUserDefault LaunchView , which the user selected last.

+11
source share

Updated for Swift 3

 func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { window = UIWindow(frame: UIScreen.main.bounds) window?.makeKeyAndVisible() window?.rootViewController = UINavigationController(rootViewController: ViewController()) return true } 
+6
source share

First of all, programmatically launching the initial controller of the presentation of your application can be done in application:didFinishLaunchingWithOptions: using the code exposed in the question:

  • set initial view control in appdelegate - fast

You can manage all the conditions that you want inside to show one or another UIViewController in the conditions database.

For example, let's say you want your application to show the game progress only the first time the application is installed, and after that go through another login screen, and then another, but you will only show the game progress for the first time and login in the case due to the fact that he was not registered earlier, and the other -.

This can be handled in several ways, I'm just trying to explain to you one way to do this.

To do this, you can install a controller called SplashViewController , for example, its initial UIViewController and inside it you will show the application image (large image logo, start screen) and your process when you go to a particular place. This way you clear the code inside AppDelegate , and it is easier to do unit test for it.

Now, if you want to switch to another UIViewController from another UIViewController , you can do this using the following code:

 let storyboard = UIStoryboard(name: "Main", bundle: nil) let viewController = storyboard.instantiateViewControllerWithIdentifier("ControllerName") as! ControllerName self.presentViewController(viewController, animated: true, completion: nil) 

Hope this helps you.

+2
source share

All Articles