简体   繁体   中英

iOS MMDrawerController objective-c login view controller

I am begin to study iOS and I try to do left navigation with MMDrawerController my AppDelegate didFinishLaunchingWithOptions code is: - (BOOL)application:(UIApplication *)application

didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

        UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];


        UIViewController *leftView = [mainStoryboard instantiateViewControllerWithIdentifier:@"LeftViewController"];
        UINavigationController *leftNav = [[UINavigationController alloc]initWithRootViewController:leftView];



            UIViewController *centerView = [mainStoryboard instantiateViewControllerWithIdentifier:@"CenterViewController"];
            UINavigationController *centerNav = [[UINavigationController alloc]initWithRootViewController:centerView ];
            self.drawerController = [[MMDrawerController alloc] initWithCenterViewController:centerNav leftDrawerViewController:leftNav];



        self.drawerController.openDrawerGestureModeMask = MMOpenDrawerGestureModePanningCenterView;
        self.drawerController.closeDrawerGestureModeMask = MMCloseDrawerGestureModePanningCenterView;
        self.window.rootViewController = self.drawerController;
        [self.window makeKeyAndVisible];


        // Override point for customization after application launch.
        return YES;
}

So it's work fine, but I have LoginViewController on my app, and if user has no saved token on NSUserDefaults , I must show LogionViewController. Of course side menu must be hidden on LoginViewController .

I Tried to switch to LoginViewController inside my CenterViewController :

- (void)viewDidLoad {
    [super viewDidLoad];

    LoginViewController * vc = [[LoginViewController alloc] init];
    AppDelegate *app = [[UIApplication sharedApplication] delegate];
    [app.drawerController setCenterViewController:vc withCloseAnimation:YES completion:nil];



}

But I have black screen only. What I do wrong? Thanks

What you're doing is a bit weird because you are setting the new centerViewController (of type LoginViewController ) within the current one (of type CenterViewController ), and once that is done the latter one will be deallocated because there are no more references to it. This might somehow be causing the black screen.

One solution would be to have the LoginViewController outside the MMDrawerController , and always present it at the beginning. If there is no token, then quickly (without animation) present the MMDrawerController and the LoginViewController won't even be seen. This way also allows you to easily dismiss back to the login screen if the user logs out.

Another option is to just present your LoginViewController from the CenterViewController modally (or however you like really) using presentViewController:animated:completion: , and then just dismiss it when they log in.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM