如何将值从单独的 类 传递到 AppDelegate?
How to Pass Values from Separate Classes to AppDelegate?
我的 SignInViewController.swift 中有一个 class:
class CredentialState: NSObject {
static let sharedInstance = CredentialState()
var signedIn = false
var displayName: String?
var photoUrl: NSURL?
}
我想使用变量 signedIn 通过 if-else 语句对 AppDelegate 中的用户进行身份验证。目前,我有办法将 viewcontroller 设置为 CustomTabBarController(自定义编程制作) 或 SignInViewController(制作故事板)。 if 语句基本上会说如果值为 false,则将控制器设置为登录屏幕,如果为 true,则转到标签栏屏幕。
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
FIRApp.configure()
// Override point for customization after application launch.
window = UIWindow(frame: UIScreen.mainScreen().bounds)
window?.makeKeyAndVisible()
// Main view controller is inside of customtabbarcontroller, which gives a tab overlay
// window?.rootViewController = CustomTabBarController()
// Sets the main view to a storyboard element, such as SignInVC
let storyboard = UIStoryboard(name: "SignIn", bundle: nil)
let loginVC = storyboard.instantiateViewControllerWithIdentifier("SignInVC") as! SignInViewController
self.window?.rootViewController = loginVC
return true
}
如果我理解正确的话:
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
FIRApp.configure()
// Override point for customization after application launch.
window = UIWindow(frame: UIScreen.mainScreen().bounds)
// check login state
if CredentialState.sharedInstance.signedIn {
// Main view controller is inside of customtabbarcontroller, which gives a tab overlay
window?.rootViewController = CustomTabBarController()
} else {
// Sets the main view to a storyboard element, such as SignInVC
let storyboard = UIStoryboard(name: "SignIn", bundle: nil)
let loginVC = storyboard.instantiateViewControllerWithIdentifier("SignInVC") as! SignInViewController
window?.rootViewController = loginVC
}
window?.makeKeyAndVisible()
return true
}
我不太确定你在问什么,但我会尽力回答。基本上你需要做的只是简单地将这段代码放在你的 CredentialState class:
之上
credentialState : CredentialState = CredentialState()
通过这种方式,您可以从 AppDelegate 更改或检查您的 signedIn 变量。因此,只需在 AppDelegate 文件中,您就可以:
if(credentialState.signedIn == true) ...
希望我能够回答你的问题
我的 SignInViewController.swift 中有一个 class:
class CredentialState: NSObject {
static let sharedInstance = CredentialState()
var signedIn = false
var displayName: String?
var photoUrl: NSURL?
}
我想使用变量 signedIn 通过 if-else 语句对 AppDelegate 中的用户进行身份验证。目前,我有办法将 viewcontroller 设置为 CustomTabBarController(自定义编程制作) 或 SignInViewController(制作故事板)。 if 语句基本上会说如果值为 false,则将控制器设置为登录屏幕,如果为 true,则转到标签栏屏幕。
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
FIRApp.configure()
// Override point for customization after application launch.
window = UIWindow(frame: UIScreen.mainScreen().bounds)
window?.makeKeyAndVisible()
// Main view controller is inside of customtabbarcontroller, which gives a tab overlay
// window?.rootViewController = CustomTabBarController()
// Sets the main view to a storyboard element, such as SignInVC
let storyboard = UIStoryboard(name: "SignIn", bundle: nil)
let loginVC = storyboard.instantiateViewControllerWithIdentifier("SignInVC") as! SignInViewController
self.window?.rootViewController = loginVC
return true
}
如果我理解正确的话:
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
FIRApp.configure()
// Override point for customization after application launch.
window = UIWindow(frame: UIScreen.mainScreen().bounds)
// check login state
if CredentialState.sharedInstance.signedIn {
// Main view controller is inside of customtabbarcontroller, which gives a tab overlay
window?.rootViewController = CustomTabBarController()
} else {
// Sets the main view to a storyboard element, such as SignInVC
let storyboard = UIStoryboard(name: "SignIn", bundle: nil)
let loginVC = storyboard.instantiateViewControllerWithIdentifier("SignInVC") as! SignInViewController
window?.rootViewController = loginVC
}
window?.makeKeyAndVisible()
return true
}
我不太确定你在问什么,但我会尽力回答。基本上你需要做的只是简单地将这段代码放在你的 CredentialState class:
之上credentialState : CredentialState = CredentialState()
通过这种方式,您可以从 AppDelegate 更改或检查您的 signedIn 变量。因此,只需在 AppDelegate 文件中,您就可以:
if(credentialState.signedIn == true) ...
希望我能够回答你的问题