1

I have a problem again. I work with storyboard in Xcode 6 with Swift as programming language. Before I want to present a view (with a view controller) on starting my app I want to test the internet connection and my server connection. If both connections are available I want to present a view1 and if not I want to present a view2. But I don't want to show a view with a spinner while checking the connection. So I thought I can manage it over the AppDelegate class. In the function func application() I want to decide which view (view1 or view2) is loaded at first. But for this solution I have to instantiate two view controllers that are connected to my two views in storyboard. I don't know if it is possible.

So my question, is it possible to instantiate these two specific storyboard view controllers in my AppDelegate class? And if it is possible, how can I do this by code?

If it is not possible, how can I solve my problem? At the moment I always show a view controller with a spinner (view0) and if connection is available I go to view1 and if connection is not available I go to view2 from my view0 controller like this:

override func viewDidAppear(animated: Bool) { //some code self.presentViewController(view1, animated: true, completion: nil) //some other code }

GRme
  • 2,707
  • 5
  • 26
  • 49
  • I think this is exactly what you are looking for http://stackoverflow.com/questions/11607024/how-to-load-different-view-controller-class-from-app-delegate-at-the-time-of-app and http://stackoverflow.com/questions/8186375/storyboard-refer-to-viewcontroller-in-appdelegate – KD. Oct 12 '14 at 15:10
  • I found these links too, but I want to do this with Swift and not with Objective-C. – GRme Oct 12 '14 at 20:03

1 Answers1

2

You can load a specific view controller from a storyboard with this call:

let viewController = self.window?.rootViewController?.storyboard?.instantiateViewControllerWithIdentifier("SomethingViewController") as UIViewController
self.window?.rootViewController = viewController

But to be honest, it doesn't seem good to make the user wait with no information while you do the connection tests.

It would be better to show a temporary view controller during the waiting, unless those tests are really fast :)


Edit: corrected the code.

It's a hackish solution, as it will load a new rootViewController after initializing the original one I suppose. But it should work.

A more correct alternative would be creating the project as "empty application" and loading the storyboard by code, directly in the controller you want.

Tiago Lira
  • 2,543
  • 1
  • 18
  • 17
  • But I can't instantiate a view controller in AppDelegate by using your code. It's possible in another view controller, but not in AppDelegate. – GRme Oct 12 '14 at 15:41