ios - 侧菜单 View 未显示,AppDelegate 类中缺少什么代码?

标签 ios objective-c sidebar appdelegate side-menu

在阅读问题之前,请参阅 this video ,这样您就会了解应用程序中的问题。

我想做的是,

情况 1:如果用户未登录应用程序,则打开应用程序登录 View 后将打开,在提供用户名和密码后,它将导航到主页。

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

在主页中,单击左侧导航按钮后,侧菜单 View 未打开/显示。

AppDelegate 类中缺少什么代码?

Here is the full source code

最佳答案

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

关于ios - 侧菜单 View 未显示,AppDelegate 类中缺少什么代码?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50677713/

相关文章:

ios - 如何从特定的 future 日期起每两周触发一次本地通知

ios - 尝试从后台恢复时触发 AVAudioSessionInterruptionNotification 和 AVAudioSessionInterruptionWasSuspendedKey

iphone - Objective-C 解密 AES 128 cbc 十六进制字符串

android - Android Studio 的结构侧边栏中的不同图标和符号表示什么意思?

ios - 我如何在 iOS swift 中对我的自定义 FBSDK 登录按钮进行操作

iphone - 倾斜显示文字

jquery - Bootstrap 4 移动导航栏从左侧滑动

overlay - 有没有办法阻止图像显示在不透明覆盖层之上?

ios - CocoaPods pod 安装。无法导入模块

objective-c - 创建将穿过主视图的标签或 UIView