侧边菜单视图未显示,AppDelegate class 中缺少什么代码?

Side-menu view is not showing, what code is missing in AppDelegate class?

在阅读问题之前请先看this video,这样您就会理解应用程序中的issue/problem。

我想做的是,

案例 1:如果用户未登录应用程序,则在打开应用程序登录视图后,将打开应用程序登录视图,在输入用户名和密码后,它将导航至主页.

情况 2:如果用户已经登录,则在启动应用程序后将显示主页。

我写了代码来存档这个,

在登录页面中,登录按钮的代码

- (IBAction)loginButton:(id)sender {

    [self->userdefaults setBool:YES forKey:@"loginSuccess"];
    [userdefaults synchronize];

    HomeViewController *home=[self.storyboard instantiateViewControllerWithIdentifier:@"homeId"];

    [self.navigationController pushViewController:home animated:YES];

}

我在 NSUserDefaults 中存储了一个值,在重新启动或打开应用程序后,在 AppDelegate class 我正在检查 NSUserDefaults 具有该值的条件, 如果是那么它会显示直接主页如果不是那么它会登录页面。

检查下面写在 AppDelegate class,

中的代码
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.

    userdefaults=[NSUserDefaults standardUserDefaults];

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


    if ([[NSUserDefaults standardUserDefaults] boolForKey:@"loginSuccess"]) {
        NSLog(@"Login Done!!!");

        HomeViewController *homeVC=[mainStoryboard instantiateViewControllerWithIdentifier:@"homeId"];

        SampleNavigationController *navigation = [[SampleNavigationController alloc] initWithRootViewController:homeVC];

       // SWRevealViewController * vc= [[SWRevealViewController alloc]init];

      // ExpandableTableViewController *sidemenu = (ExpandableTableViewController*)[mainStoryboard instantiateViewControllerWithIdentifier:@"sideMenu"];

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

    }else
    {

        LoginViewController *loginVC=[mainStoryboard instantiateViewControllerWithIdentifier:@"LoginId"];

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

    }

    return YES;
}

在首页点击左侧导航按钮后侧边菜单视图不是opening/showing。

AppDelegate class 中缺少什么代码?

Here is the full source code

侧面菜单视图不是 opening/showing,因为您没有初始化 SWRevealViewController 并且 rootViewController 不是 SWRevealViewController

工作代码。

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.

    userdefaults=[NSUserDefaults standardUserDefaults];

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


    if ([[NSUserDefaults standardUserDefaults] boolForKey:@"loginSuccess"]) {
        NSLog(@"Login Done!!!");

        HomeViewController *homeVC=[mainStoryboard instantiateViewControllerWithIdentifier:@"homeId"];
        SampleNavigationController *navigation = [[SampleNavigationController alloc] initWithRootViewController:homeVC];

        ExpandableTableViewController *sidemenu = (ExpandableTableViewController*)[mainStoryboard instantiateViewControllerWithIdentifier:@"sideMenu"];

        // Initialize SWRevealViewController and set it as |rootViewController|
        SWRevealViewController * vc= [[SWRevealViewController alloc]initWithRearViewController:sidemenu frontViewController:navigation];

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

    }else
    {

        LoginViewController *loginVC=[mainStoryboard instantiateViewControllerWithIdentifier:@"LoginId"];
        UINavigationController* navLogin = [[UINavigationController alloc] initWithRootViewController:loginVC];

        self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
        // Using UINavigationController here because you use
        // pushViewController:animated: method in loginButton:
        self.window.rootViewController = navLogin;
        [self.window makeKeyAndVisible];

    }

    return YES;
}

您需要稍微更改 loginButton: 方法以使其在第一时间起作用。

- (IBAction)loginButton:(id)sender {
    [self->userdefaults setBool:YES forKey:@"loginSuccess"];
    [userdefaults synchronize];

    HomeViewController *home=[self.storyboard instantiateViewControllerWithIdentifier:@"homeId"];

    SampleNavigationController *navigation = [[SampleNavigationController alloc] initWithRootViewController:home];

    ExpandableTableViewController *sidemenu = (ExpandableTableViewController*)[self.storyboard instantiateViewControllerWithIdentifier:@"sideMenu"];
    SWRevealViewController * vc= [[SWRevealViewController alloc]initWithRearViewController:sidemenu frontViewController:navigation];

    [self presentViewController:vc animated:YES completion:nil];
}