我正在使用xcode/swift创建一个应用程序,它实现了一个滑动的侧边栏菜单以及一个登录/注册过程。滑动侧边栏菜单功能应该只能在完成登录或注册后立即在下一个视图上访问。
我在这里遵循了滑动边栏教程的说明: Ray Wenderlich边栏教程
问题是,只有当包含的视图是打开应用程序时显示的第一个视图时,-> My Sidebar菜单功能才有效。
有时这是可以的,因为一旦用户登录,侧边栏菜单视图就是显示的第一个视图--但情况并不总是如此--注销的用户必须完成注册/登录,然后才能访问/转换到此视图。
我将应用程序的初始视图设置为在故事板中指向我的LoginViewController,所以我不知道为什么它不能先加载!?
我猜这与在我的AppDelegate.swift文件中设置根视图控制器有关。
AppDelegate.swift
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(application: UIApplication!, didFinishLaunchingWithOptions launchOptions: NSDictionary!) -> Bool {
window = UIWindow(frame: UIScreen.mainScreen().bounds)
let containerViewController = ContainerViewController()
window!.rootViewController = containerViewController
window!.makeKeyAndVisible()
return true
}如何强制在应用程序启动时首先显示loginViewController!?
发布于 2016-08-01 14:35:20
首先,您需要将rootViewController设置为loginVC,因此在appDelegate文件中,将上述方法替换为
func application(application: UIApplication!, didFinishLaunchingWithOptions launchOptions: NSDictionary!) -> Bool {
window = UIWindow(frame: UIScreen.mainScreen().bounds)
// replace names accordingly
let loginViewController = LoginViewController()
window!.rootViewController = loginViewController
window!.makeKeyAndVisible()
return true
}现在,您应该将用户的状态保存在userDefaults或其他文件中。例如,如果用户未登录
NSUserDefaults.standardUserDefaults().setBool(false, forKey: "isUserLoggedIn)当用户登录到应用程序时,您可以像这样更新userDefault
NSUserDefaults.standardUserDefaults().boolForKey("isUserLoggedIn")因此,您的最终函数应该如下所示
func application(application: UIApplication!, didFinishLaunchingWithOptions launchOptions: NSDictionary!) -> Bool {
window = UIWindow(frame: UIScreen.mainScreen().bounds)
if NSUserDefaults.standardUserDefaults().boolForKey("isUserLoggedIn") == true{
let containerViewController = ContainerViewController()
window!.rootViewController = containerViewController
window!.makeKeyAndVisible()
}
else {
let loginViewController = LoginViewController()
window!.rootViewController = loginViewController
window!.makeKeyAndVisible()
}
return true
}https://stackoverflow.com/questions/38689532
复制相似问题