Ios 侧菜单视图未显示,AppDelegate类中缺少哪些代码?
在阅读问题之前,请查看,以便您了解应用程序中的问题 我想做的是 案例1:如果用户未登录应用程序,则在打开应用程序后,将打开登录视图,并在提供用户名和密码后,将导航到主页 案例2:如果用户已登录,则在启动应用程序后,它将显示主页 我写了代码来存档这个 在登录页面中,输入登录按钮的代码Ios 侧菜单视图未显示,AppDelegate类中缺少哪些代码?,ios,objective-c,sidebar,appdelegate,side-menu,Ios,Objective C,Sidebar,Appdelegate,Side Menu,在阅读问题之前,请查看,以便您了解应用程序中的问题 我想做的是 案例1:如果用户未登录应用程序,则在打开应用程序后,将打开登录视图,并在提供用户名和密码后,将导航到主页 案例2:如果用户已登录,则在启动应用程序后,它将显示主页 我写了代码来存档这个 在登录页面中,输入登录按钮的代码 - (IBAction)loginButton:(id)sender { [self->userdefaults setBool:YES forKey:@"loginSuccess"]; [u
- (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
是否具有该值,如果是,则它将显示direct homepage,如果不是,则它将登录页面
检查以下在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;
}
在主页中,单击左侧导航按钮后,侧菜单视图未打开/显示
AppDelegate
类中缺少什么代码
侧菜单视图未打开/显示,因为您未初始化
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可能存在一些问题,单击侧面菜单按钮上的“注销”按钮后,它不会删除值。它的最佳代码是什么?单击login后,我想在NSUserDefaults中存储一个值,以便在打开应用程序后,我将签入AppDelegate类,以便如果该值存在,则显示主页else登录页。从Apple的文档中…
synchronize()
“…此方法是不必要的,不应使用。”