Last active
September 6, 2018 10:04
-
-
Save ishaq/7749853 to your computer and use it in GitHub Desktop.
iOS: load main interface programmatically
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
- (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; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
} |
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.
Thank you so much for your post.
thanks for this)
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
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!