Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AppDelegate, rootViewController and presentViewController

I'm doing the Facebook integration tutorial, I want to show my MainViewViewController if the user has a valid token for the current state otherwise I want to show LoginViewController.

MainViewAppDelegate.m

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {     if (FBSession.activeSession.state == FBSessionStateCreatedTokenLoaded) {         // To-do, show logged in view     } else {         // No, display the login page.         [self showLoginView];     }     return YES; } - (void)showLoginView {     UIStoryboard *mainstoryboard = [UIStoryboard storyboardWithName:@"MainStoryboard"          bundle:nil];     LoginViewController* loginViewController = [mainstoryboard      instantiateViewControllerWithIdentifier:@"LoginViewController"];     [self.window.rootViewController presentViewController:loginViewController animated:YES completion:NULL]; } 

Console error :

Warning: Attempt to present <LoginViewController: 0xb492fd0> on <MainViewViewController: 0xb1bd820> whose view is not in the window hierarchy! 

I don't want to use a NavigationController.

like image 310
Boris-V Avatar asked Nov 17 '12 11:11

Boris-V


People also ask

Is Appdelegate a controller?

The application delegate is a controller object. By default, it is the owner and controller of the main window -- which is a view -- in an iOS app. The app delegate receives messages from an object representing -- or modeling -- the application itself (an instance of UIApplication ).


1 Answers

I had the same issue. Based on the answer to this question, I added [self.window makeKeyAndVisible] just before presentViewController:animated:completion:, and that fixed it for me.

In your case, showLoginView becomes

- (void)showLoginView {     UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];     LoginViewController *loginViewController = [storyboard instantiateViewControllerWithIdentifier:@"LoginViewController"];     [self.window makeKeyAndVisible];     [self.window.rootViewController presentViewController:loginViewController animated:YES completion:NULL]; } 
like image 141
shebang Avatar answered Sep 19 '22 21:09

shebang