在SWRevealViewController之前登录View控制器

如果注册或登录成功,我按照parse.com教程了解如何实现login + register + segue到主控制器。 这很美。

然后我想使用SWRevealViewController实现侧面导航。 我在此关注了APPCODA链接,只有在“SWRevealViewController”是初始视图控制器时才能使用它。

当我将我的解析登录控制器作为初始时,我无法从导航控制器“SWRevealViewController”中看到任何内容。

我怎么能解决这个问题,让我的登录/注册控制器成为初始控制器,并且在登录/注册成功时仍然能够拥有SWRevealViewController?

我将不胜感激任何帮助或指示。

下面是我的APPDelegate.m

#import "AppDelegate.h" #import  #import  @interface AppDelegate () @end @implementation AppDelegate - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { LoginView *lv = [[LoginView alloc]init]; SidebarViewController *sbvc = [[SidebarViewController alloc]init]; UINavigationController *nav = [[UINavigationController alloc]initWithRootViewController:lv]; UINavigationController *menuVC = [[UINavigationController alloc]initWithRootViewController:sbvc]; SWRevealViewController *revealController = [[SWRevealViewController alloc]initWithRearViewController:menuVC frontViewController:nav]; revealController.delegate = self; self.menu = revealController; self.window.rootViewController = self.menu; [GMSServices provideAPIKey:@"XXXXXXXXXX"]; [Parse setApplicationId:@"XXXXXXXXXXXXX" clientKey:@"XXXXXXXXXXXXX"]; [PFAnalytics trackAppOpenedWithLaunchOptions:launchOptions]; return YES; } 

在具有用户会话的应用程序中,通常最好将登录/注册保持在与应用程序其余部分不同的视图层次结构中,并在存在活动会话时显示内部结构。 您可以通过检查来实现此目的

 [PFUser currentUser] 

因为如果用户没有登录,它将返回nil

像这样检查您的AppDelegate:

 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { // .. Rest of your initialization code if ([PFUser currentUser]) { // Let's pop them right to the home screen [self showHomeScreen]; } else { // Present login vc LoginView *lv = [[LoginView alloc] init]; UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:lv]; self.window.rootViewController = nav; } return YES; } - (void)showHomeScreen { SidebarViewController *sbvc = [[SidebarViewController alloc] init]; UINavigationController *menuVC = [[UINavigationController alloc] initWithRootViewController:sbvc]; UIViewController *home = [[UIViewController alloc] init]; UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:home]; SWRevealViewController *revealController = [[SWRevealViewController alloc] initWithRearViewController:menuVC frontViewController:nav]; self.window.rootViewController = revealController; } 

然后,通过包括

 - (void)showHomeScreen; 

AppDelegate.h ,您可以在注册/登录成功后调用此方法:

 AppDelegate *delegate = (AppDelegate *)[[UIApplication sharedApplication] delegate]; [delegate showHomeScreen]; 

我只是想方设法地从app delegate显示我的仪表板视图。 使用SWRevealViewController和swift 2.0:

在你的main.storyboard上,你必须识别菜单视图(我的意思不是导航控制器)

对于我的右侧菜单,身份检查员 – > identiy-> storyboardID =’right2’。

对于我的左侧菜单,称为’rear2’。

对于我的前视图,在显示我的仪表板视图的导航视图控制器上,我将标识设置为“仪表板”

在你的appdelegate中,在我的函数’didFinishLaunchingWithOptions’下:

 //this is your main.storyboard let storyboard = UIStoryboard(name: "Main", bundle: nil) //initialize your FRONT view controller, ie my dashboard view, mine is a UINavigationController, which then presents a table view , ie why i instantiate with a UINavigationController. let frontviewcontroller = storyboard.instantiateViewControllerWithIdentifier("dashboard") as? UINavigationController //initialize REAR View Controller- it is the LEFT hand menu. let rearViewController = storyboard.instantiateViewControllerWithIdentifier("rear2") as? UITableViewController //initialize RIGHT View Controller - it is your RIGHT hand menu. let rightViewController = storyboard.instantiateViewControllerWithIdentifier("right2") as? UITableViewController let mainRevealController = SWRevealViewController() mainRevealController.frontViewController = frontviewcontroller mainRevealController.rightViewController = rightViewController mainRevealController.rearViewController = rearViewController self.window!.rootViewController = mainRevealController self.window?.makeKeyAndVisible() 

希望它能帮助别人。 谢谢D.

好的,我明白了。 它在这里;

而不是创建一个segue到主视图控制器,创建它到SWRevealViewController。 我认为SW将该控制器设置为“sw_front”。 只有通过PFUser将其作为segue才能使用。

以下是用户登录后我换出导航层次结构的方法:

UINavigationController版本

 + (void)postSignInUICodeFromViewController:(UIViewController *)vc { // Log in was successful // Create new view controller ViewController1 *v1 = [[ViewController1 alloc] init]; typeof(vc) weakVc = vc; // This is a property that I have added to my // custom UIViewController subclass. As you might // guess, it's called in "viewDidAppear:" v1.viewDidAppearCompletionBlock = ^{ // After pushing the new UI hierarchy into view, // this will delete/remove the rest of the UI stack weakVc.navigationController.viewControllers = @[v1]; }; // At this point (since the block above hasn't yet fired), // you're pushing your new UI hierarchy ON TOP OF the login // view controller [vc.navigationController pushViewController:v1 animated:YES]; } 

UITabBarController版本

 + (void)postSignInUICodeFromViewController:(UIViewController *)vc { // Log in was successful // Create new UI hierarchy ViewController1 *v1 = [[ViewController1 alloc] init]; NavViewController *nav1 = [[UINavController alloc] initWithRootViewController:v1]; ViewController1 *v2 = [[ViewController2 alloc] init]; NavViewController *nav2 = [[UINavController alloc] initWithRootViewController:v2]; ViewController1 *v3 = [[ViewController3 alloc] init]; NavViewController *nav3 = [[UINavController alloc] initWithRootViewController:v3]; ViewController1 *v4 = [[ViewController4 alloc] init]; NavViewController *nav4 = [[UINavController alloc] initWithRootViewController:v4]; TabBarViewController *tabBar = [[TabBarViewController alloc] init]; tabBar.viewControllers = @[nav1, nav2, nav3, nav4]; typeof(vc) weakVc = vc; typeof(tabBar) weakTabBar = tabBar; __weak NavViewController *navVc = (NavViewController *)weakVc.navigationController; // This is a property that I have added to my // custom UITabBarController subclass. As you might // guess, it's called in "viewDidAppear:" tabBar.viewDidAppearCompletionBlock = ^{ // After pushing the new UI hierarchy into view, // this will delete/remove the rest of the UI stack weakVc.navigationController.viewControllers = @[weakTabBar]; }; // At this point (since the block above hasn't yet fired), // you're pushing your new UI hierarchy ON TOP OF the login // view controller [vc.navigationController pushViewController:tabBar animated:YES]; } 

在这两个例子中,我组成了一个名为ViewController1的新类。 在UITabBarController示例中,所有其余内容也由类名组成,仅用于说明目的。 你需要输入你的类名。

在您的登录ViewController中创建按钮Action。

你可以像这样设置。

  @IBAction func btnLoginAction(_ sender: UIButton) { let objAppDelegate = UIApplication.shared.delegate as! AppDelegate let storyboard = UIStoryboard(name: "Main", bundle: nil) let rearViewController = storyboard.instantiateViewController(withIdentifier: "backTableVc") as? backTableVc let frontViewController = storyboard.instantiateViewController(withIdentifier: "ViewController") as? ViewController let naviRoot = UINavigationController(rootViewController: frontViewController!) naviRoot.navigationBar.isHidden = true let mainRevealController = SWRevealViewController() mainRevealController.frontViewController = naviRoot mainRevealController.rearViewController = rearViewController UIView.transition(from: (objAppDelegate.window?.rootViewController!.view)!, to: mainRevealController.view, duration: 0.6, options: [.transitionCrossDissolve], completion: { _ in objAppDelegate.window?.rootViewController = mainRevealController }) objAppDelegate.window?.makeKeyAndVisible() }