[英]Login View controller before SWRevealViewController
我遵循parse.com教程,了解如何在注册或登录成功后实现登录+注册+对主控制器的segue。 这很漂亮。
然后,我想使用SWRevealViewController实现侧面导航。 我遵循了APPCODA链接,并且仅当“ SWRevealViewController”是初始视图控制器时,该链接才起作用。
当我将解析登录控制器作为初始名称时,我无法从导航控制器(即“ SWRevealViewController”)看到任何内容。
登录/注册成功后,我将如何解决此问题,将我的登录/注册控制器设置为初始控制器,并且仍然能够拥有SWRevealViewController?
我将不胜感激任何帮助或指针。
以下是我的APPDelegate.m
#import "AppDelegate.h"
#import <Parse/Parse.h>
#import <GoogleMaps/GoogleMaps.h>
@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];
我只是想知道如何以编程方式从应用程序委托显示仪表板视图。 使用SWRevealViewController和swift 2.0:
在main.storyboard上,您将必须确定菜单视图(我不是导航控制器)
对于我的右侧菜单,身份检查器-> identiy-> storyboardID ='right2'。
在我的左侧菜单中称为“ rear2”。
对于我的前视图,在显示我的仪表板视图的导航View控制器上,我将标识设置为“仪表板”
在您的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。
以下是用户登录后如何换出导航层次结构的方法:
+ (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];
}
+ (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
示例中,所有其余部分也由类名组成,仅出于说明目的。 您将需要输入您的类名。
好,所以我想通了。 它去了;
与其在主视图控制器上创建序列,不如在SWRevealViewController上创建序列。 我发现SW将该控制器设置为“ sw_front”。 仅当您通过PFUser将其作为序列时,这才起作用。
在您的登录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()
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.