我是 Swift 和 Storyboard 的新手。我正在构建一个应用程序,需要在用户成功登录后保持登录状态。 因此,在用户第一次打开应用程序时,我将显示登录 ViewController,用户登录后,应用程序将显示主页 ViewController。
对于 Objective-C 和 xib,我只是将用户登录数据保存到数据库,每次用户打开应用程序时,我都会检查 AppDelegate 中是否存在用户数据,如果存在,我将显示 HomeVC.xib,否则我显示登录VC.xib。这是正确的方法吗?
如何使用 Storyboard 来做到这一点?
谢谢
最佳答案
您可以通过从第一个 View Controller 中取消选中“是初始 View Controller ”选项来以编程方式设置初始 View Controller like so , 或者只需删除指向第一个 View Controller 的箭头即可。 然后为所有想要作为初始 View Controller 的 View Controller 提供不同的 Storyboard ID, Like that .
然后在 AppDelegate 类中的 application:didFinishLaunchingWithOptions 方法中手动将适当的 View Controller 设置为初始 View Controller :
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
// Override point for customization after application launch.
window = UIWindow(frame: UIScreen.mainScreen().bounds)
let storyboard = UIStoryboard(name: "Main", bundle: nil)
window?.rootViewController = storyboard.instantiateViewControllerWithIdentifier((isUserLoggedIn ? "MainVC" : "LoginVC"))
window?.makeKeyAndVisible()
return true
}
关于iphone - iOS Swift Storyboard 如何在登录后和登录前显示 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39157355/