How to call a view controller from AppDelegate in iOS

appdelegate, ios, viewcontroller, xcode

Solution

The code you are currently using is completely deleting the root view controller of your app window, in other words, you are deleting all the views and view controllers in your app. If you are not using ARC, this is just one huge memory leak. If you are, it's still not a very good idea.

In your `applicationWillEnterForeground:` method, try using this code instead:

RoomRootViewController* room = [[RoomRootViewController alloc] init];
[self.window.rootViewController presentViewController:room
                                             animated:NO
                                           completion:nil];

This will display the `RoomRootViewController` over the top of all your app's current views, instead of deleting them. You can then dismiss it like this:

[self.window.rootViewController dismissViewControllerAnimated:YES
                                                   completion:nil];

and easily return to the rest of your app.

Problem

I am creating an iOS app in which I have the following requeriment: the app should show the login screen when it starts the first time and also that screen must also be shown when the app comes from the background to the foreground, in case it has been sent to background during run time. I have handled to show the screen in both cases. However, when I do it and the app comes from the background, and I click the texfield to type my password, the app gets frozen, and it fails in a thread that I don't know what it means. I call the screen to be shown when the app comes from background like this, in the applicationWillEnterForeground, in AppDelegate: ``` self.window=[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; RoomRootViewController* room = [[RoomRootViewController alloc] init]; [[self window] setRootViewController:room]; [self.window makeKeyAndVisible]; ``` Is this the correct way to do so? Thanks a lot in advance ! I am completely lost with this as I am very new in iOS, so any help will be very appreciated. Attached in an image you can see where the app fails.

Original source