I have a login view controller which on a correct login will open a tabbar controller with the following method:
self.dismissViewControllerAnimated(true, completion: { self.loadHomeScreen()})
func loadHomeScreen()
{
emailField.text = ""
passwordField.text = ""
self.presentViewController(UIStoryboard.tabbarController()!, animated: true, completion: nil)
}
private extension UIStoryboard {
class func mainStoryboard() -> UIStoryboard { return UIStoryboard(name: "Main", bundle: NSBundle.mainBundle()) }
class func tabbarController() -> UITabBarController? {
return mainStoryboard().instantiateViewControllerWithIdentifier("TabbarControllerID") as? UITabBarController
}
}
I think my login view controller is still in the background? I have a logout button as the rightbarbutton item on each navbar in my tabgroup. I want to be able to close the tabbar on this button press. How can I achieve this. I can't find any examples. Would I be using a pop command?
UPDATE:
#IBAction func tryLogout(sender: UIBarButtonItem) {
self.dismissViewControllerAnimated(true, completion: nil)
let storyboard = UIStoryboard(name: "main", bundle: nil)
let vc = storyboard.instantiateViewControllerWithIdentifier("login") as! UIViewController
self.presentViewController(vc, animated: true, completion: nil)
}
Related
I'm trying to fix an onboarding/walkthrough to my app. Everything works as it should, except that when I navigate to the first page, navigationController disappears.
When I close the app and start it again, there is NavigationController/bar there.
is there something wrong in my code?
#IBAction func buttonPressed(_ sender: Any) {
let storyborad = UIStoryboard(name: "Main", bundle: nil)
let mainVC = storyborad.instantiateViewController(withIdentifier: "mainVC") as! ViewController
self.present(mainVC, animated: true, completion: nil)
}
You need to present your VC with a UINavigationController, or push a new VC onto the current navigationController.
First approach pushing mainVC to current navigationController (Probably will work better in your case):
#IBAction func buttonPressed(_ sender: Any) {
let storyborad = UIStoryboard(name: "Main", bundle: nil)
let mainVC = storyborad.instantiateViewController(withIdentifier: "mainVC") as! ViewController
self.navigationController?.pushViewController(mainVC, animated: true)
}
Second approach, presenting with initializing a navigationController:
#IBAction func buttonPressed(_ sender: Any) {
let storyborad = UIStoryboard(name: "Main", bundle: nil)
let mainVC = storyborad.instantiateViewController(withIdentifier: "mainVC") as! ViewController
self.present(UINavigationController(rootViewController: mainVC), animated: true, completion: nil)
}
This is my code to navigate different navigation controllers in my app.
I have a modal view to present the menu and later select where navigate,
this is a good approach?
func circleMenu(_: CircleMenu, buttonDidSelected _: UIButton, atIndex: Int) {
weak var pvc = self.presentingViewController
if atIndex == 1 {
self.dismiss(animated: true) {
let storyboard = UIStoryboard(name: "Experiencia", bundle: nil)
let vc = storyboard.instantiateViewController(withIdentifier: "ExperienciaNavigationController")
pvc?.present(vc, animated: true, completion: nil)
}
}
if atIndex == 2 {
self.dismiss(animated: true) {
let storyboard = UIStoryboard(name: "Servicios", bundle: nil)
let vc = storyboard.instantiateViewController(withIdentifier: "ServiciosNavigationController")
pvc?.present(vc, animated: true, completion: nil)
}
}
if atIndex == 3 {
self.dismiss(animated: true) {
let storyboard = UIStoryboard(name: "Book", bundle: nil)
let vc = storyboard.instantiateViewController(withIdentifier: "DemoViewController")
pvc?.present(vc, animated: true, completion: nil)
}
}
}
And later in other vc that was selected I have this code to return to root
func back() {
self.view.window?.rootViewController?.dismiss(animated: true, completion: nil)
}
And the final question its about the navigation controller that will be closed... Will be closed all the views presented? to avoid memory issues.
I read about implement a coordinator pattern. but I don't know if this case to do that Coordinator pattern or Navigator
I am trying to move back from a ViewController (simple text page) to a main ViewController with a TabBar at the bottom using gesturing.
The gesture works as I return to the original main screen but there is no TabBar.
In the simple ViewController I use this code to go back to the originating ViewController.
#objc func respondToSwipeGesture(gesture: UIGestureRecognizer) {
if let swipeGesture = gesture as? UISwipeGestureRecognizer {
switch swipeGesture.direction {
case UISwipeGestureRecognizerDirection.right:
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let controller = storyboard.instantiateViewController(withIdentifier: "AboutViewControllerID")
self.present(controller, animated: true, completion: nil)
if let vc = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "AboutViewControllerID") as? AboutViewController
{
present(vc, animated: true, completion: nil)
}
default:
break
}
}
}
In the ViewController with the TabBarController I have tried the following lines to rejuvinate the TabBarController but without any joy.
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(true)
tabBarController?.tabBar.isHidden = false
NSLog("TabBar true")
}
Any ideas?
First if you want to go-back don't use present as it will add the same VCS twice in the stack , you have to use unwindSegue/dismiss , or load the tabBar itself with id
self.dismiss(animated: true) {
// use this if it's not directly behind the dismissed VC
let tab = storyboard.instantiateViewController(withIdentifier: "tabBarID") as! UITabBarController
UIApplication.shared.keyWindow?.rootViewController = tab
}
//
OK, i have 2 problem here:
1
I try to load a ViewController from a button in the xib file.
This is my button from my xib: UIView.
#IBAction func fechaSalida(_ sender: Any) {
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let vc = storyboard.instantiateViewController(withIdentifier: "calendario") as! CalendarioViewController
self.presentViewController(vc, animated: true, completion: nil)
}
The problem here is the presentViewController because my xib is a UIView type, his show me this error: "(value type ViewController) has no member of presentViewController".
Why i try to use this code? is because in my CalendarioViewController i have this code when pressed a button: dismiss(animated: true, completion: nil) so this is my first problem...
2
The second problem is when i have this code in a button inside of my xib: UIView, the CalendarioViewController is load ok...but the code: dismiss(animated: true, completion: nil) is not working anymore.
Here my code of my button xib:
#IBAction func fechaSalida(_ sender: Any) {
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let vc:CalendarioViewController = storyboard.instantiateViewController(withIdentifier: "calendario") as! CalendarioViewController
vc.dateDelegate = self
self.window!.rootViewController = vc
}
So..how can load a ViewController (CalendarioViewController) and the dismiss code Keep running?
ok... i have what i need.
For this work i use UITapGestureRecognizer in View Controller which holds the view:
In override func viewDidLoad() put this code:
let fechaArriboTap = UITapGestureRecognizer(target: self, action:#selector(handleArribo))
let fechaSalidaTap = UITapGestureRecognizer(target: self, action:#selector(handleSalida))
fechaArribo.addGestureRecognizer(fechaArriboTap)
fechaSalida.addGestureRecognizer(fechaSalidaTap)
Then create the methods:
func handleArribo() {
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let vc:CalendarioViewController = storyboard.instantiateViewController(withIdentifier: "calendario") as! CalendarioViewController
vc.dateDelegate = self
present(vc, animated: true, completion: nil)
}
func handleSalida() {
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let vc:CalendarioViewController = storyboard.instantiateViewController(withIdentifier: "calendario") as! CalendarioViewController
vc.dateDelegate = self
present(vc, animated: true, completion: nil)
}
and thats is! Thanks #sasquatch
I have a view displaying some content, which is password protected. In viewWillAppear a variable gets checked, to see if the user is properly logged in :
override func viewWillAppear(animated: Bool) {
if (!Config.userLoggedIn) {
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let loginVC = storyboard.instantiateViewControllerWithIdentifier("loginVC") as! UIViewController
loginVC.modalTransitionStyle = UIModalTransitionStyle.CrossDissolve
self.navigationController!.presentViewController(loginVC ,animated: true, completion: nil)
}
}
It works, but the content underneath is visible for a short fraction of a second. How can I present the loginVC without revealing the content underneath.
I cannot put it in viewDidLoadbecause all this is part of a TabBarController, and the views might already be in memory and viewDidLoad is only called once
Simple solution. Hide whole view in viewDidLoad then show it back in viewViewAppear if user is correctly logged in
override func viewDidLoad(){
self.view.hidden = true
}
override func viewWillAppear(animated: Bool) {
if (!Config.userLoggedIn) {
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let loginVC = storyboard.instantiateViewControllerWithIdentifier("loginVC") as! UIViewController
loginVC.modalTransitionStyle = UIModalTransitionStyle.CrossDissolve
self.navigationController!.presentViewController(loginVC ,animated: true, completion: nil)
}else{
self.view.hidden = false
}
}