【问题标题】:Side-menu view is not showing, what code is missing in AppDelegate class?侧菜单视图未显示,AppDelegate 类中缺少什么代码?
【发布时间】:2018-11-13 14:52:24
【问题描述】:

在阅读问题之前,请先查看this video,以便您了解应用程序中的问题/问题。

我想做的是,

案例 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 类中,我正在检查NSUserDefaults 具有该值的条件,如果是,那么它将显示直接主页,否则它将登录页面。

查看下面AppDelegate类编写的代码,

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

在首页,点击左侧导航按钮后侧边菜单视图没有打开/显示。

AppDelegate 类中缺少什么代码?

Here is the full source code

【问题讨论】:

标签: ios objective-c sidebar appdelegate side-menu


【解决方案1】:

侧边菜单视图未打开/显示,因为您没有初始化 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];
}

【讨论】:

  • 很棒的解决方案@trungduc 先生。我在这里感到困惑,但您在几分钟内提供了答案。
  • 如果我的回答解决了您的问题,请随时投票并将其标记为正确答案;)
  • 侧边菜单正在打开,完全没有问题。
  • 当我第一次登录应用程序时,侧边菜单没有打开,然后当重新启动应用程序时它会显示。 NSUserDefaults 可能存在一些问题,单击侧面菜单按钮上的注销按钮后它不会删除值。
  • 最好的代码是什么?单击登录后,我想在 NSUserDefaults 中存储一个值,以便在打开应用程序后检查 AppDelegate 类,如果该值存在则显示主页其他登录页面。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-03-20
  • 2010-11-04
  • 1970-01-01
  • 1970-01-01
  • 2017-04-18
  • 1970-01-01
相关资源
最近更新 更多