ios - performSegue before the View is visible -
i have 2 views
viewcontroller-> main view loginvc -> login view my initial view viewcontroller contains buttons , text.
what want achive
perform segue transfer view login if user not yet log in.
what have done
inside viewcontroller did check if user_id nil if nil perform segue.
override func viewwillappear(animated: bool) { if globals.user_id == nil{ self.performseguewithidentifier("goto_login", sender: nil) // dispatch_async(dispatch_get_main_queue(), { // self.performseguewithidentifier("goto_login", sender: nil) // }) } } what problem
my problem whether use viewwillappear or viewdidload or viewdidappear transfer view viewcontroller loginvc.viewcontroller visible in single second before the login screen appears , want rid of it.can me solve issue.
i have same functionality in app. achieved checking userid in appdelegate.swift inside function func application(application: uiapplication, didfinishlaunchingwithoptions launchoptions: [nsobject: anyobject]?) -> bool
so, delete segue , modified code follows:
func application(application: uiapplication, didfinishlaunchingwithoptions launchoptions: [nsobject: anyobject]?) -> bool { // override point customization after application launch. let storyboard: uistoryboard = uistoryboard(name: "main", bundle: nsbundle.mainbundle()) if globals.user_id == nil{ let loginscreen = storyboard.instantiateviewcontrollerwithidentifier("loginvc") self.window?.rootviewcontroller = loginscreen }else{ let mainscreen = storyboard.instantiateviewcontrollerwithidentifier("viewcontroller") self.window?.rootviewcontroller = mainscreen } return true } this way not need of method viewdidload(), viewdidappear() or viewwillappear()to used purpose.
just addition answer - use instance of appdelegate again after logout happens. example should following in logout method:
func logout() { if let appdelegate = uiapplication.sharedapplication().delegate as? appdelegate { globals.user_id = nil appdelegate.window?.rootviewcontroller = loginscreen } } this way can clear userid , navigate login screen after user logouts.
Comments
Post a Comment