Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@ishaq
Last active September 6, 2018 10:04
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save ishaq/7749853 to your computer and use it in GitHub Desktop.
Save ishaq/7749853 to your computer and use it in GitHub Desktop.
iOS: load main interface programmatically
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
UIStoryboard *mainStoryboard = nil;
BOOL showLogin = NO;
if(showLogin == NO)
{
// mainStoryboard method looks like:
// return [UIStoryboard storyboardWithName:@"Main" bundle:nil];
mainStoryboard = [UIStoryboard mainStoryboard];
}
else
{
mainStoryboard = [UIStoryboard loginStoryboard];
}
UIViewController *rootController = [mainStoryboard instantiateInitialViewController];
NSAssert(rootController, @"no user interface, what is this? a web service??");
self.window.rootViewController = rootController;
[self.window makeKeyAndVisible];
return YES;
}
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
self.window = UIWindow(frame: UIScreen.mainScreen().bounds)
var mainStoryboard: UIStoryboard? = nil
let showTutorial = true
if(showTutorial) {
mainStoryboard = UIStoryboard.tutorialStoryboard()
}
else {
// mainStoryboard method looks like:
// return UIStoryboard(name: "Main", bundle: nil)
mainStoryboard = UIStoryboard.mainStoryboard()
}
let rootController = mainStoryboard!.instantiateInitialViewController()
assert(rootController != nil, "no user interface, must be the D day")
self.window?.rootViewController = rootController
self.window?.makeKeyAndVisible()
return true
}
@kenmux
Copy link

kenmux commented Nov 25, 2015

AppDelegate.m, line 3: What's the intent of creating a new window?
The app ran into some problems when I use this code snippet, comment that line, all's OK!

@ishaq
Copy link
Author

ishaq commented Jan 28, 2016

Sorry @kenmux, didn't see your comment till now (guess GH doesn't post notifications on comments to Gists). I was creating a window myself because I have not set any storyboard to be the Main Interface, If you don't set it, you are responsible for creating the window yourself, and since you are loading appropriate storyboard through your code anyway, setting a Main Interface didn't make any sense either.

@rajeshpatel-vl
Copy link

Thank you so much for your post.

@abesmon
Copy link

abesmon commented Oct 17, 2017

thanks for this)

@harvdogg
Copy link

harvdogg commented Apr 4, 2018

Note that as of Swift 4 that UIScreen.mainScreen().bounds (line 2) has been renamed to UIScreen.main.bounds

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment