Modal viewcontroller UI not responsive after presentViewController:animated:completion:

大憨熊 提交于 2020-02-25 04:40:08

问题


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!

回答1:


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;
}


来源:https://stackoverflow.com/questions/17679659/modal-viewcontroller-ui-not-responsive-after-presentviewcontrolleranimatedcomp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!