Here's the situation. I have an NSTimer in Appdelegate.m that performs a method that checks the users location and logs him out if he is out of a region. When that happens I want to load the login view controller. However, with the following lines of code, I am able to see the VC but if I click on a textfield to type, I get a sigbart. Any ideas how to load the initial VC from app delegate correctly?
UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle: nil];
LoginViewController *loginViewController = (LoginViewController *)[mainStoryboard instantiateViewControllerWithIdentifier:@"Login"];
[self.window addSubview:loginViewController.view];
[self.window makeKeyAndVisible];
I've been struggling with this quite a bit. Here's what worked for me
UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle: nil];
LoginViewController *loginViewController = (LoginViewController *)[mainStoryboard instantiateViewControllerWithIdentifier:@"Login"];
//set the root controller to it
self.window.rootViewController = loginViewController
If you're still having errors, it has to do with your LoginViewController, not the AppDelegate transition. Hope this helps.
Both audiophilic and Flaviu 's approaches are ok. But both of you are missing one extra line of code.
I found out that you have to allocate and init the UIWindow object first. I dont know why but initializing the object solved the problems.
self.window=[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
audiophilic's approach:
self.window=[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle: nil];
ResultsInListViewController *resultsInListViewController = (ResultsInListViewController *)[mainStoryboard instantiateViewControllerWithIdentifier:@"ResultsInListViewController"];
[self.window addSubview:resultsInListViewController.view];
[self.window makeKeyAndVisible];
Flaviu's approach:
self.window=[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle: nil];
ResultsInListViewController *resultsInListViewController = (ResultsInListViewController *)[mainStoryboard instantiateViewControllerWithIdentifier:@"ResultsInListViewController"];
[self.window setRootViewController:resultsInListViewController];
[self.window makeKeyAndVisible];
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With