I have seen many posts similar to this here but they are all about Objective-C while I am developing my app in Swift. As you can see from the image I have a login screen view and I correctly implemented the login mechanism.
Now I would like that when the login has succedeed, then the tab bar controller is showed. In my login view controller I have this function for login:
var finalURL:NSString = "\(Settings.webServerLoginURL)?username=\(username)&password=\(password)" LoginService.requestLoginWithURL(NSURL(string: finalURL as String)!, completionHandler: { (success) -> Void in if (success) { NSLog("Login OK") /* Scarica dal database i tasks di LoggedUser.id */ /* Redirect al tab HOME dell'applicazione dove si mostrano il numero di task di quell'utente ed in cima "BENVENUTO: name surname" */ } else { self.alertView.title = "Autenticazione fallita!" self.alertView.message = "Username o passowrd." self.alertView.delegate = self self.alertView.addButtonWithTitle("OK") self.alertView.show() }
So I think that I should show tab bar controller after
NSLog("Login OK")
but I don't know how. I am a beginner of Swift/XCode...if you can explain me. Thanks to all who have read.
To present a ViewController in a NavigationController you can wrap ViewController into a NavigationController as it is in the example below. Then present NavigationController which contains ViewController. Check out the below video courses to learn more about Mobile App Development for iOS platform with Swift.
To show tab bar controller from Login page, connect the Login page and TabbarController with a Show segue and give it an identifier in attributes inspector (Say "mySegueIdentifier").
To add segue, just right click and drag from Login view controller to TabbarController.
In successful Login you can simply call "performSegueWithIdentifier" method as follows
self.performSegue(withIdentifier: "mySegueIdentifier", sender: nil)
In your case you call it after this line.
NSLog("Login OK")
If you don't want to navigate from Login page to TabbarController, you can also set it as rootViewController after successful Login. To do this, set an identifier to TabbarController (Say "myTabbarController")
let appDelegate = UIApplication.shared.delegate! as! AppDelegate let initialViewController = self.storyboard!.instantiateViewController(withIdentifier: "myTabbarControllerID") appDelegate.window?.rootViewController = initialViewController appDelegate.window?.makeKeyAndVisible()
Edit:
Swift 3
let appDelegate = UIApplication.shared.delegate! as! AppDelegate let initialViewController = self.storyboard!.instantiateViewController(withIdentifier: "myTabbarControllerID") appDelegate.window?.rootViewController = initialViewController appDelegate.window?.makeKeyAndVisible()
Happy coding.. :)
I ran into this same issue when trying to segue from a controller I used for touchID to a TabBarController. By making the segue in an async block I resolved the issue.
dispatch_async(dispatch_get_main_queue(), { self.dismissViewControllerAnimated(false, completion: {}) self.performSegueWithIdentifier("authnToAppSegue", sender: nil) })
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With