How to load a login controller from an application delegate

Here is the situation. I have NSTimer in Appdelegate.m that executes a method that checks the location of a user and logs him out if he is outside the region. When this happens, I want to load the login controller. However, with the following lines of code, I can see the VC, but if I click on the text box to print, I get a sigbart. Any ideas on how to properly load the source VC from the application delegate?

UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle: nil]; LoginViewController *loginViewController = (LoginViewController *)[mainStoryboard instantiateViewControllerWithIdentifier:@"Login"]; [self.window addSubview:loginViewController.view]; [self.window makeKeyAndVisible]; 
+4
source share
3 answers

I struggle a lot with this. 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 still have errors, this is due to your LoginViewController and not to the AppDelegate application. Hope this helps.

+11
source

Both audio and Flaviu approaches are fine. But you are missing one extra line of code.

I found out that you need to first select and run the UIWindow object. I don’t know why, but initializing the object solved the problems.

 self.window=[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; 

audiophile 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]; 

Flavi:

 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]; 
+4
source

This is the code I use when I want to change the views:

 #import "LoginViewController.h" LoginViewController *screen = [[LoginViewController alloc] initWithNibName:nil bundle:nil]; screen.modalTransitionStyle = UIModalTransitionStyleCrossDissolve; [self presentModalViewController:screen animated:YES]; //or [self.window addSubView:screen]; 

Using this code, it will create a new instance and show it, so it should fulfill your question. You may need to move some things, but they should work.

Let me know if you need more help.

Hope this helps you!

+1
source

Source: https://habr.com/ru/post/1401260/


All Articles