Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to load a login view controller from app delegate

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];
like image 810
audiophilic Avatar asked Mar 13 '12 22:03

audiophilic


2 Answers

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.

like image 82
Flaviu Avatar answered Sep 26 '22 01:09

Flaviu


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];
like image 40
Shuaib Avatar answered Sep 24 '22 01:09

Shuaib