2016-06-20 25 views
1

我有一個類在我SignInViewController.swift:如何將值從單獨的類傳遞到AppDelegate?

class CredentialState: NSObject { 
    static let sharedInstance = CredentialState() 
    var signedIn = false 
    var displayName: String? 
    var photoUrl: NSURL? 
} 

我想用變量signedIn在AppDelegate中使用if-else語句驗證用戶身份。目前,我有一種方法將視圖控制器設置爲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 
} 

回答

1

如果我理解正確的話你:

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 
} 
+0

這完美地工作。謝謝! – About7Deaths

+0

不客氣。很高興我能幫上忙 :) –

1

我不太知道自己在問什麼,但我會盡力回答。基本上你需要做的是隻讓這段代碼你CredentialState類以上:

credentialState : CredentialState = CredentialState() 

這樣你可以改變或從AppDelegate中檢查signedIn變量。所以在AppDelegate的簡單文件,您可以:

if(credentialState.signedIn == true) ... 

希望我能回答你的問題

相關問題