简体   繁体   English

iOS MMDrawerController Objective-C登录视图控制器

[英]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 我开始研究iOS,我尝试使用MMDrawerController进行左侧导航,我的AppDelegate didFinishLaunchingWithOptions代码为:-( 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. 这样就可以了,但是我的应用程序上有LoginViewController ,如果用户在NSUserDefaults上没有保存的令牌,则必须显示LogionViewController。 Of course side menu must be hidden on LoginViewController . 当然,侧面菜单必须在LoginViewController上隐藏。

I Tried to switch to LoginViewController inside my CenterViewController : 我试图切换到CenterViewController内部的LoginViewController:

- (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. 您正在执行的操作有点怪异,因为您正在当前( CenterViewController类型)中设置新的centerViewController (类型LoginViewController ),并且一旦完成,将取消分配后者,因为没有更多引用了。 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. 一种解决方案是将LoginViewController放在MMDrawerController之外,并始终在开始时显示它。 If there is no token, then quickly (without animation) present the MMDrawerController and the LoginViewController won't even be seen. 如果没有令牌,则快速(不带动画)显示MMDrawerController ,甚至看不到LoginViewController 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. 另一个选择是使用presentViewController:animated:completion:以模态方式(或您确实喜欢)从CenterViewController呈现LoginViewController ,然后在他们登录时将其关闭。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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