Dismissing the tabbar view controller in swift - swift

I have loginViewController and I am pushing a tabBarViewcontroller which is has 3 tabs
Below's my code:
self.dismiss(animated: false, completion: nil)
var profileStoryBoard: UIStoryboard!
profileStoryBoard = UIStoryboard(name:"Login", bundle: nil)
let viewcontroller: MainViewController = profileStoryBoard.instantiateViewController(withIdentifier: "MainViewController") as! MainViewController
self.navigationController?.pushViewController(viewcontroller, animated: false)
I then dismiss the loginViewController and push the tabBarViewController.
Now I have a sign out button on all 3 tabBar items, but it is not working.
Here's my code:
var profileStoryBoard: UIStoryboard!
profileStoryBoard = UIStoryboard(name:"Main", bundle: nil)
let viewcontroller : LoginViewController = profileStoryBoard.instantiateViewController(withIdentifier: "LoginViewController") as! LoginViewController
self.navigationController?.pushViewController(viewcontroller, animated: false)
I am new to swift so I don't know what mistake I might be doing here.

What you are doing is pushing another controller on top of the stack
self.navigationController?.pushViewController(viewcontroller, animated: false)
You instead want to go back one label. You should use this:
navigationController?.popViewController(animated: true)

Related

NavigationController PushedView Controller dismisses entire NavigationController - swift - Programmatically

I'm presenting a NavigationController modally like this:
let profileViewController = ProfileViewController(nibName: nil, bundle: nil)
let navigationControllerProfile = UINavigationController(rootViewController: profileViewController)
navigationControllerProfile.setNavigationBarHidden(true, animated: false)
navigationControllerProfile.modalPresentationStyle = .fullScreen
self.present(navigationControllerProfile, animated: true, completion: nil)
Whenever I push a Viewcontroller from this NavigationController:
let showCaseViewController = ShowcaseViewController()
showCaseViewController.modalPresentationStyle = .overFullScreen
self.navigationController?.pushViewController(showCaseViewController, animated: true)
The ViewController is pushed correctly but after 1 second the entire NavigationController is dismissed.
code to present view-controller with navigation
let vc = storyboard?.instantiateViewController(withIdentifier: "firstvc") as! firstvc
let navigation = UINavigationController(rootViewController: vc)
vc.modalPresentationStyle = .fullScreen
self.present(navigation, animated: true, completion: nil)
code for perform push operation inside presented view controller
let vc = storyboard?.instantiateViewController(withIdentifier: "email") as! email
self.navigationController?.pushViewController(vc!, animated: true)

error instantiating a viewcontrolelr modally via code

why with this code, outlets in second view controller are unwrapped as the were nil crashing the app? they even are not appearing. issue happens if I try to access the outlets, but not if I change the view's background.
in view controller 1 button:
let vc = SecondViewController.self.createAcertainCustomAppearenceOfVC()
vc.modalPresentationStyle = .overCurrentContext
vc.modalTransitionStyle = .crossDissolve
present(vc, animated: true, completion: nil)
in second view controller
final class func createAcertainCustomAppearenceOfVC() -> SecondViewController {
let VC = SecondViewController()
VC.view.backgroundColor = .systemRed
// VC.tappedSecondOut.setTitle("push", for: .normal)
VC.tappedSecondOut.backgroundColor = .black
return VC
}
You're trying to instantiate a view controller that has outlets in a storyboard, you need to instantiate the controller from both the storyboard name and the view controller's identifier that is set in Interface Builder.
final class func createAcertainCustomAppearenceOfVC() -> SecondViewController? {
let storyboard = UIStoryboard.init(name: "Main", bundle: nil)
return storyboard.instantiateViewController(withIdentifier: "secondViewControllerIdentifier") as? SecondViewController
}

Swift: Gesturing doesn't bring back the TabBar

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
}
//

Open the VC by pressing a button from the XIB

Help me please.
There is a map with markers, clicking on them brings up a XIB = UIVIew, there is a button in it, you need to click the button to open the viewController
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let vc = storyboard.instantiateViewController(withIdentifier: "MarkerVC")
self.navigationController?.pushViewController(vc, animated: true)
let viewController = YourViewController(nibName: "YourViewController", bundle: nil)
self.present(viewController, animated: true, completion: nil)
If the VC is from XIB, use the above.

How to correctly display a tab controller in swift

I'm making a login screen for my app and everything works as intented until I try to present my main view after the login(which uses a Tab Bar Controller).
The only problem is that it displays just the first item on the tab bar. I have to press the other buttons for the to appear.
Im using this code:
//after login...
var storyboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
var vc: TabBarViewController = storyboard.instantiateViewControllerWithIdentifier("MainController") as! TabBarViewController
self.presentViewController(vc, animated: true, completion: nil)
My guess is that I need to load them all at the same time, but I dont know...
This is how I did it recently. I loaded my tabBarController and the login screen together, once the user has logged in (or completed the first screen experience) you can modally dismiss the controller.
func showloginView() {
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let loginViewController: LoginTableViewController = storyboard.instantiateViewControllerWithIdentifier("LoginTVC") as! LoginTableViewController
self.window?.makeKeyAndVisible()
var viewController = storyboard.instantiateViewControllerWithIdentifier("LoginTVC") as! LoginTableViewController
let nav = UINavigationController(rootViewController: viewController)
self.window?.rootViewController?.presentViewController(nav, animated: true, completion: nil)
}
for displaying your tabBarController and editing of any of the tabBarItems
let tabBarController = self.window?.rootViewController as? UITabBarController
let tabBarRootViewControllers: Array = tabBarController!.viewControllers!
let nav = tabBarRootViewControllers[0] as? UINavigationController
Hope this helps =)
If you link an UIButton, you can open a UITabBarController using this (providing you're using a Storyboard)
#IBAction func openTabBar(sender: AnyObject) {
var tabBarController = self.storyboard?.instantiateViewControllerWithIdentifier("tabBarController") as! UITabBarController
self.presentViewController(tabBarController, animated: false, completion: nil)
}
That'll pop the current view and open the tab bar controller.