2015-10-30 34 views
0

嗨,我有一個登錄視圖控制器和我的應用程序故事板視圖。初始視圖控制器指向應用程序儀表板。我正在檢查我的appDelegate應用程序功能,如果用戶登錄。之後,我(重新)設置正確的UIViewController作爲初始。不知何故,該應用程序總是去初始視圖控制器,然後說我需要去登錄,因爲用戶沒有登錄...我怎樣才能避免這種情況?決定應用程序完成加載前的初始視圖控制器

這是我現在的代碼。

dispatch_async(dispatch_get_main_queue(), { 
    let storyboard: UIStoryboard = UIStoryboard.init(name: "Main", bundle: nil) 
    if (settings.isLoggedIn()) 
    { 
     let vc: AnyObject! = storyboard.instantiateInitialViewController() 
     self.window?.rootViewController = vc as? UIViewController 
    } else { 
     let vc: AnyObject! = storyboard.instantiateViewControllerWithIdentifier("login") 
     self.window?.rootViewController = vc as? UIViewController 
    } 
}) 
+1

爲什麼dispatch_async? – Leonardo

+1

@Leonardo實際上是一個非常好的,我認爲xcode會抱怨在沒有確定它在主線程上的情況下使用界面做東西。我刪除它,現在流量真的很流暢。我認爲那是我的錯誤。 – Reshad

+0

這是在輔助線程上做UI的問題,但appDelegate方法總是在主線程上調用,所以你不必擔心那裏 – Leonardo

回答

0

如何檢查用戶登錄或沒有在下面的方法中的appdelegate

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool { 

    //check user logged or not, if true you can set the initial view controller given below 

    let storyboard = UIStoryboard(name: "Main", bundle: nil) 
    let vc : UIViewController! = storyboard.instantiateViewControllerWithIdentifier("urcontroller") 

    let window = UIApplication.sharedApplication().windows[0] ; 
    window.rootViewController = vc; 

    return true 
} 
相關問題