Question

My app has a root viewcontroller, which at the start of the app displays

  • login viewController view if the user is not logged in
  • main viewController view if the user is logged in

AppDelegate code:

- (BOOL)              application: (UIApplication*) application
    didFinishLaunchingWithOptions: (NSDictionary*) launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame: [[UIScreen mainScreen] bounds]];

    self.window.rootViewController = [[RootViewController alloc] init];

    [self.window makeKeyAndVisible];

    return YES;
}

Here's the code used in RootViewController:

@implementation RootViewController

- (void) loadView
{
   [super loadView];

   // here mainViewController and loginNavigationController are initialized
}

...

- (UIView*) view
{   
   [super view]; // this invokes loadView

    return self.isLoggedIn ? self.mainViewController.view : 
                             self.loginNavigationController.view;
}

....

- (void) userDidLogin
{
     [self.loginNavigationController presentViewController: self.mainViewController 
                                                  animated: YES 
                                                completion: nil];
}
@end

If the user is not logged in and presses login button the main viewController is presented.

The problem is that after main viewController is presented, I'm not able to interact with any of the UI elements. For example, I have a tableView as a main viewController's subview and when I try to scroll it I get the following warning in debug panel:

 <UITableView: 0x202a4000; frame = (0 0; 310 548); clipsToBounds = YES;
 gestureRecognizers = <NSArray: 0x1fd9f570>; layer = <CALayer: 0x1fdccff0>;
 contentOffset: {0, 0}>'s window 
 is not equal to <RootViewController: 0x1fd9f7d0>'s view's window!
Was it helpful?

Solution

Ok, so after looking at the updated code I see that you have a rootViewController and are dynamically giving the view you think should be presented. The thing is, the rootViewController is in charge of the root view while your other two view controllers manage their own views. You should not be passing a different view controller's view off.

So in the end it looks like you want to conditionally set your rootviewcontroller. So lets look at the app delegate. I think you should make your app delegate do something like this. Have it figure out at runtime which viewcontroller to present. Then make that the rootviewcontroller for the app.

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    UIViewController * resolvedRootViewController = [self someMethodThatCorrectlyGivesRootViewController];

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

    return YES;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top